How to Create and Use Bash Scripts19 Jan 2019
This is meant to be a short guide to getting started with making your first script, and learning some basic bash syntax. Bash is available by default on Linux and macOS operating systems.
This guide is for macOS. I’ll be using
/Users/laurenceb for all examples, but it will be
/Users/your_username for you.
Now on your mac open the Terminal.
In this tutorial, we’re going to create a bash script that can be run from any directory on the computer.
Create a bin directory.
The first step is to create a bin directory. bin is the standard naming convention of a subdirectory that contains executable programs. bin stands for binary.
/bin is a location for binary files which are programs and commands. note its not the only location where binary files can be stored
Navigate to your home directory
~ (which is a shortcut for current user home directory, or
pwd will confirm your location.
Create bin in that folder, or wherever you want your bash scripts to live.
Export your bin directory to the PATH.
.bash_profile, which will be located at
/Users/laurenceb/.bash_profile, and add this line to the file. If
.bash_profile doesn’t exist, create it.
After exporting the path
export PATH=$PATH:/Users/laurenceb/bin, it’s best to reload the
source ~/.bash_profile. Otherwise you can restart the shell/terminal or open a new tab.
If you don’t see hidden files and directories, or those that begin with a
Create a script file and make it executable
Go to your bin folder located in
A bash script must always begin with
#!/bin/bash to signify that the script should run with bash as opposed to any other shell. This is called a “shebang”. heck which bash interpreter you have in your terminal:
A bash script must always begin with
#!/bin/bash to signify that the script should run with bash as opposed to any other shell. This is called a “shebang”.
As is tradition, we’ll make a “Hello, World!” example to get this working.
Now, you can try to run the file in the terminal but you would get
-bash: hello-world: command not found
We have to make it an executable file by changing the permissions.
Now when you run the command, it will output the contents of the echo.
You just got your first bash script up and running. You can also run this script from anywhere on the computer, not just in the bin directory. If the bash entry to declare the PATH is not correct you will only be able to execute the file with
./hello-world in the same directory.
Strings do not need to use single or double quotes by default. However, single and double quoted strings work as well. A single quoted string will not interpolate variables, but a double quoted string will. Variables A variable is declared without a $, but has a $ when invoked. Let’s edit our hello-world example to use a variable for the entity being greeted, which is World.
who = "World" is not valid – there must not be a space between variable and value.
We declared a variable in the last example, but we can also have the user set the value of a variable dynamically. For example, instead of just having the script say Hello, World!, we can make it ask for the name of the person calling the script, then output that name. We’ll do this using the read command.
if statements use the if, then, else, and fi keywords. The condition goes in square brackets.
Operators are slightly different in bash.
|-ge||>=||Greater than or equal|
|-le||<=||Less than or equal|
Bash uses for, while, and until loops. In this example, I’ll use the for…in loop to get all the files in a directory and list them.
Git Deploy Example Script
A bash script can use any commands you can use on the command line. An example of a script you might make for yourself is the one below, where the user is prompted for a git commit message and the process of adding, committing, and pushing to origin is all done with a single command.
The read command:
-r do not allow backslashes to escape any characters
-p prompt output the string PROMPT without a trailing newline before
man read in terminal to see more!
Then just run the command
push_git in terminal
You can always use add
./ before the file name (e.g.
./hello-world) to run the script from the current directory.
I hope this article has been helpful for you to get started with bash scripting.If you liked this post, you can share it with your followers or follow me on Twitter!