Autoyast: Scripts being executed in POSIX mode inspite of being executed with Bash

Here’s another tip related to AutoYast and scripts such as pre-install scripts, indirectly related to the previous post on error reporting.

One of the errors you might see in the dialog enabled there with the %lt;feedback%gt; tag could be something like this:

your_script.sh: line 13: syntax error near unexpected token `< '
your_script.sh: line 13: `readarray DISKS < <( echo -e "${DISKS_STRING}" | sort -n )'

This uses two Bash-specific features, the readarray command and process substitution. One might think that this shouldn't be a problem since the installation system provides Bash and all you need is the #!/bin/bash shebang in the first line of the script. Nope, doesn't help. But why? Let's for a change have a look at the Yast sources!

The nice thing is not only that Yast sources are Open source, they are also readily available at GitHub for closer inspection. Using the installation system's /var/log/YaST2/y2log it is rather easy to derive the source file in question, yast-autoinstallation/src/modules/AutoinstScripts.rb, where we can find the following code:

[...]
                interactiveScript(
                  "/bin/sh",
                  debug,
[...]

So your scripts do not get called directly, that’s why the #!/bin/bash shebang doesn’t help. They get called with /bin/sh, not /bin/bash. Now you could argue that with /bin/sh being a symlink to /bin/bash there shouldn’t be a difference, but there actually is! Even though it is the fully-featured Bash, it if gets called as /bin/sh Bash will operate in POSIX mode in which it confirms more closely to the POSIX standard and disables Bash-specific features. And since the call to our script is hardcoded into Yast, we can not change that our scripts get called that way.

Don’t worry, though, the solution to this problem is so simple it could be patented: POSIX mode can be turned on and off from within your script with the set command. Just begin your scripts with

set +o posix

and voila, all your Bashisms are again available!