Say there's a script that makes use of a certain Bash feature, like arrays. But say that script is being run using sh
on Ubuntu, which is Dash, a shell that does not support arrays. Let's also assume I can't control which shell is being used from the get go, i.e. I'm stuck with Dash to start the script with.
Is it possible to detect and switch away from Dash (or any other shell) to Bash, assuming the location of Bash binary is known and can be safely assumed to be consistent? E.g. I run the following script using sh myscript.sh
:
#!/bin/sh
# Do something here to detect Dash and switch to Bash (/bin/bash)
my_array=(yay i can use arrays now)
# ...
Please note that the script must be run using sh myscript.sh
and I cannot update it to make it bash myscript.sh
.
Bash always sets BASH_VERSION
. You can check if it's set and restart the script with bash otherwise like this:
if [ -z "$BASH_VERSION" ]; then
exec bash "$0" "$@"
fi
my_array=(yay i can use arrays now)
# ...