Shell Script Examples

Sourcing bash scripts

source my_script

and

. my_script

are equivalent. This is like copying and pasting each line from the file into your terminal emulator.

Running bash scripts

you can run a bash script with the following command:

bash my_script

that does not mean it is executable. to do that, you need to add a “shebang” with the executable’s filepath to the top of the script. For example,

#!/bin/bash

But that may not be the path of your bash program. How do you know what to put there? What program do you want to run? To find out the path of the program, use

which bash

ensure the script is executable:

chmod +x <filename>

then run the script

./my_script

Other Shell Script Notes

How to find the directory of the script

#!/bin/bash
MY_PATH="`dirname \"$0\"`"

$MY_PATH/relative/path/to/other/file

pass arguments on to another script

$@

for example

#!/bin/bash
MY_PATH="`dirname \"$0\"`"

python3 $MY_PATH/my-script.py "$@"