How can my script determine whether it's being run by bash or dash?
Solution 1:
You can make the fact that $BASH_VERSION
is blank in dash
work for you:
if [ "$BASH_VERSION" = '' ]; then
echo "This is dash."
else
echo "This is bash."
fi
Solution 2:
You just have to use quotes on the variable BASH_VERSION
to use -n
if [ -n "$BASH_VERSION" ];then
echo "this is bash";
else
echo "this is dash";
fi
Solution 3:
Use /proc/[PID]/cmdline
to see what the script is being run with and test for what it contains. The $$
variable will give us the PID of the running shell. Thus we can make a script like this,
#!/bin/bash
if grep -q 'bash' /proc/$$/cmdline ;
then
echo "This is bash"
else
echo "This is some other shell"
fi
Here's a test of the same script:
$> bash test_script.sh
This is bash
$> dash test_script.sh
This is some other shell