-
Notifications
You must be signed in to change notification settings - Fork 6
Description
To implement
- Emit
if status is-interactive
oncase $- in *i*)
Description
This may be a specific use case, but is valuable in a lot of scenarios.
Consider the following script:
f() {
echo 'f function you also want to source'
}
case $- in
*i*) return
esac
main() {
f
}
main
In the case
statement, we check if the shell is interactive. If it is, the script exits early. This allows the f()
function to be imported into our environment without needing to manually add it to .profile
. As a result, the script can be executed or sourced without extra redundancy.
Most of my scripts, if not all, take this form, as it allows them to gracefully handle being both sourced and executed without duplicating helper functions in the shell .profile
.
Currently, babelfish
will try to transliterate it as-is:
switch "$-"
case '*i*'
return
end
That previous code will fail, but iwe can achieve a similar result inn fish
with:
if status is-interactive
return
end
We could emit that code whenever a simple case $- in *i*)
is detected.
Any thoughts or feedback are welcome! 💙