Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX and Bash Scripts

If I want to use the non-POSIX [[ and (( test forms in my scripts, and I declare the shell I want use with

#!/bin/bash

at the beginning of my scripts, will calling the Bash shell directly prevent any problems that might arise from using those forms? Or will some other shell automatically be used if the shell referenced in the hash-bang isn't found?

like image 779
Victor Brunell Avatar asked Jun 13 '26 17:06

Victor Brunell


1 Answers

Both Linux and OS X will refuse to execute the script if the executable specified on the shebang line is not found (haven't tried others).

You'll see something like /bin/bash: bad interpreter: No such file or directory and the exit code will be 127.

However, a more portable way of referencing the desired shell is to write the shebang line as follows (e.g., for bash):

#!/usr/bin/env bash

That way, the shell executable will be found as long as it is in the $PATH.

like image 158
mklement0 Avatar answered Jun 15 '26 06:06

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!