This page has been tested on macOS Catalina and Ubuntu 18.04 & 20.04
Bash (Linux) and Z Shell (Zsh) (macOS) are the default languages used to run commands on the Terminal. For the purposes of these tutorials you can consider them to be the same.
Bash stands for “Bourne again shell”, a double pun that derives both from the name of its inventor (Stephen Bourne) and the fact that it is a ‘born again’ version of an older shell written by Bourne. Zsh is an extended and improved version of a Bourne shell, created by Paul Falstad and named after Zhong Shao.
cd
cd
stands for ‘change directory’cd
followed by a space into the terminal, use Tab to auto-complete the name of the folder you want to change intoecho $PWD
to check your Present Working Directory, ie what folder you are currently in, and ls
to list the files in your present working directorytouch myscript.sh
myscript.sh
Note 1: while there is a convention to use the
.sh
extension for shell scripts, it is not strictly necessary and can be omitted. It can be useful, however: if you are using an IDE such as Sublime Text to edit and run the script it will let it know what type of file it is, helping with syntax highlighting and automatic building. It can also help you control what happens when the file is double-clicked (see below).
Note 2: while it is possible to have spaces in your script’s name, doing so will makes it more difficult to run and may create problems (see below). This is thus inadvisable.
Once the file myscript.sh
is created, open it and paste in the following:
# This is my first bash script
echo "Hello, World"
This is a simple script that will display the words “Hello, World”. The first line is a comment: it does nothing and will not be run because it is not part of the programme, but it is useful to have because it serves as a note to yourself. The second line displays (‘echoes’) the words between the quotation marks.
There are a number of ways to do this (if the script runs successfully, you will see the words “Hello, World” because that is what your programme ‘myscript’ does!). Note that the below is written for Bash but works equally well for Zsh:
bash myscript.sh
. This tells your computer to use the bash
programme to execute the myscript.sh
script. Similarly, if your script is in a different folder to the one your terminal is currently pointing at, you can run it with bash name_of_folder/myscript.sh
as long as there is no space in the name of the folder (see below).chmod +x myscript.sh
on the terminal. This gives your computer permission to run it directly and so it can run with /.myscript.sh
, which saves a few keystrokes.
#!/bin/bash
on line 1 of myscript.sh, which indicates that you intend to use the ‘bash’ program located in your ‘bin’ folderecho $PATH
). It can now be run from the terminal with myscript.sh
.sh
extension when you are using this methodchmod +x myscript.sh
) right click the file > Open With > Other… > at the “Enable” dropdown menu change “Recommended Applications” to “All Applications” > look for the “Utilities” folder > select “Terminal”. Now you can double-click the file to run it.source
command, eg having source myscript.sh
in a different script file inside the same folder as myscript. Running that script will cause “Hello, World” to be displayed.Note: If there is a space in your script’s name or in the name of the folder then it’s still possible to run it by using quotation marks:
bash "my script.sh"
or"./my folder/my script.sh"
, etc. However, this is not recommended. If you have spaces in folder- and filenames it may break other programmes that try to run your script or just generally make your life harder than necessary.