Thomas S.
Thomas S.

Reputation: 6365

Ensure shell script is launched with correct shell

Our shell script's first line reads

#!/bin/bash

According to my (limited) Linux shell knowledge this causes to use /bin/bash to launch the application when the user invokes this script directly, e.g., using

$ ./myscript.sh

Unfortunately, the script works not as expected, when the user launches it using

$ sh ./myscript.sh

How can I catch the case that the "wrong" shell executes this script and show the user an appropriate error message?

Upvotes: 0

Views: 73

Answers (1)

Mark Reed
Mark Reed

Reputation: 95375

case "$BASH" in
  *bash) :;;
  *) echo >&2 "$0: please run this with bash."; exit 1;;
esac

Or you could just do it for them:

case "$BASH" in
  *bash) :;;
  *) exec bash "$0" "$@";;
esac

Upvotes: 2

Related Questions