Get AppleScript’s “do shell script” to use the latest Bash

applescriptbash

For some reason, I cannot get AppleScript's "do shell script" to use the latest (GPLv3) Bash:

do shell script "/usr/local/bin/bash; echo $BASH_VERSION"
-- Result: "3.2.57(1)-release"

Strangely, it does work with an external .sh script:

#!/usr/local/bin/bash
echo $BASH_VERSION

Calling it like:

do shell script "~/Library/Scripts/Script.sh"
-- Result: "5.0.17(1)-release"

What's the difference and can I get it to work inside the .scpt script as well?

Best Answer

do shell script always uses /bin/sh, even if your default shell, shebang, or $PATH variable is set to something else.

To force it to use a different shell, you will need to call that shell. To run a command in bash, use the argument -c:

/usr/local/bin/bash -c 'echo $BASH_VERSION'

If you only want to run a single bash script, you can omit the -c with: /usr/local/bin/bash /path/to/script.bash


In AppleScript:

do shell script "/usr/local/bin/bash -c 'echo $BASH_VERSION'"

The man page entry for bash -c:

-c command_string

If the -c option is present, then commands are read from the first non-option argument command_string.