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.
scroll to the bottom to see how to integrate a bash script with GitHub.
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 if not already existing.
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.
Check git is installed on your mac!
If it is not ‘command not found’ is returning then install Xcode from the App Store.
In XCode, install
Command Line Tools: > Preferences > Downloads > Command Line Tools
I assume you have a GitHub account already set up. Go to your repository on GitHub and you will see a button:
Clone or Download. There you will have the option to select
HTTPS and get the URL. For more infos see this link: cloning a repository.
You might need to configure Git on your computer, however the global username and email as below in the git config command, need not be the same as your account. It will not be used to login but just to add a signatures to your commits: You can change the name that is associated with your Git commits using the git config command. The new name you set will be visible in any future commits you push to GitHub from the command line. If you’d like to keep your real name private, you can use any text as your Git username. Changing the name associated with your Git commits using git config will only affect future commits and will not change the name used for past commits.
Choose where you want to have the folder on your hard disk. I used my home directory.
So now you have your repo as a local folder.
Make then this script in the bin directory as save it as
push_git or another name you like
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!
Again you will need to give the correct permissions:
Restart the terminal with
or open a new window
Navigate in terminal to the local repo folder you cloned before and in that folder run the command
The first time you execute the script it will ask for your github username and password!
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!