On ubuntu I run the following script (which should randomly fail or succeed for testing purposes):
if [ "$RANDOM" -gt 16384 ]; then
echo "Success!"
exit 0
else
echo "Failure!"
exit 1
fi
with
sh random.sh
but it fails with
random.sh: 1: [: Illegal number:
Failure!
What is the cause of the problem?
The $RANDOM
variable is a bash extension, so you have to invoke your script with bash
instead of sh
. On Ubuntu, sh
is typically linked to the more light-weight dash
shell which doesn't support these extensions.
Also see Difference between sh and Bash
To make reasonably sure that the correct interpreter is used, add a shebang to your script:
#!/bin/bash
if [ "$RANDOM" -gt 16384 ]; then
echo "Success!"
exit 0
else
echo "Failure!"
exit 1
fi
and make it executable:
chmod +x random.sh
A user can now simply execute the script without specifying what interpreter it needs in order to work properly:
./random.sh