After looking around for a while I've found several good pages describing how to check and see if a bash script is running interactively and there seem to be several methods, but one of them that I keep seeing over and over is checking the variable
$- for the presence of
i to signify that it is interactive.
I'm looking for a way to use an
if statement that would work with both
/bin/sh (which is typically aliased to
/bin/dash on Ubuntu systems), or in fewer words
if test that is universal as possible"
bash it seems these things should work:
case "$-" in *i*) echo This shell is interactive ;; *) echo This shell is not interactive ;; esac
if [[ $- == *i* ]] then do_interactive_stuff fi
$- is apparently not set in
dash. Possibly because
$- is supposed to be the Flags passed to script variable which
was originally a ksh construct adopted into Bash, and unfortunately it does not seem to work reliably in Bash scripts... [ for it's intended purpose however] ...One possible use for it is to have a script self-test whether it is interactive.
So is there an (relatively simple)
if test that can be used both with
Or am I just "doing it wrong?"
ps: I have been under the impression that
dash is faster than
bash, and unless the script needs "bash specific" functions it would be better to hashbang it as
#!/bin/sh and let it use the system-default shell. I get that checking for an interactive shell in the way mentioned above is "bash specific" but it seems odd to think there is not a more universal way. In the script I'm working on I don't need any other bash-specific functionality.