Explicitly interpret a script

Running Scripts#

There are many ways to run Bash scripts, and there are some subtle differences between them.

Explicitly interpret a script#

You can explicitly interpret a script using a specific bash executable by passing the script as an argument:

The bash executable is an “interpreter,” a program which can read and execute commands written in the Bash scripting language. Basically this means that if script.bash is a readable file in the current directory you can run bash ./script.bash to execute the commands within that file.

Note that there’s no requirement that the file is a valid Bash script! For one thing, the file extension does not have any influence on the contents of a file. Someone could mv cat.jpg script.bash, but of course that doesn’t make their cat picture a valid script! Something you are more likely to come across is people using the generic .sh extension. I’ve avoided this, because despite superficial similarities it is extremely difficult to write truly portable shell scripts. In general you can’t expect a script to work the same in different versions of Bash without some work to test the functionality in every version after every change. Supporting even a single version of another shell is much more work, and actively supporting more than one shell can cripple the development process.

 

This page is a preview of The newline Guide to Bash Scripting

No discussions yet