Linux Script Error: Bad Substitution (change shell from dash to bash)
This is a simple problem in the end that cost me serious troubleshooting time. Hopefully you found this article and it can save you some of what I lost.
You may encounter a problem when running a script on the Linux CLI that results in error:
bad substitution and a failed script run.
This may be due to your distro using
dash as the shell instead of
bash and more importantly, that
sh may link to
dash which can mess you up further.
First check on your shell with:
echo $SHELL which will display your current users shell. If this shows
bash you’re likely okay there, if it does not, then you may need to update your user’s shell by issuing a command like
usermod --shell /bin/bash <username>.
Next, if your last check was okay, you need to find out where
sh is pointing so run
readlink -f $(which sh) or
ls -al /bin/sh either should give you the information you need.
If you find that the previous results show some form of path to
dash then you may need to update it. Especially if it is a symlink (which it probably is) that shows something like this:
[email protected]:~# ls -al /bin/sh lrwxrwxrwx 1 root root 4 Oct 26 01:39 /bin/sh -> dash
If you see the above symlink you’re likely going to have to manually delete and recreate the symlink pointing to bash as follows (remember to use
sudo if you are not running the command as root):
rm /bin/sh ln -s /bin/bash /bin/sh
Once you’ve knocked this out your scripts should once again run as expected.