Understanding Bashisms
A bashism is any feature that’s specific to bash
, and that won’t work with other shells. Let’s look at a few examples.
Using Portable Tests
For our first example, try running this command on your Fedora virtual machine:
donnie@fedora:~$ [[ -x /bin/ls ]] && echo "This file is installed.";
This file is installed.
donnie@fedora:~$
Here, I’m testing for the presence of the ls
executable file in the /bin/
directory. The file is there, so the echo
command is invoked. Now, let’s run the same command on a FreeBSD virtual machine:
donnie@freebsd14:~ $ [[ -f /bin/ls ]] && echo "This file is installed."
-sh: [[: not found
donnie@freebsd14:~ $
This time I get an error, because the default user login shell on FreeBSD is sh
, instead of bash
. The problem here is that the [[. . .]]
construct isn’t supported on the FreeBSD implementation of sh
. Let’s see if we can fix...