How to Create and Use Bash Scripts

Bash is a Unix shell, which is a command line interface (CLI) for interacting with an operating system (OS). Any command that you can run from the command line can be used in a bash script. Scripts are used to run a series of commands.

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 /Users/laurenceb). Typing pwd will confirm your location. Create bin in that folder, or wherever you want your bash scripts to live.

cd ~      # this takes us to /Users/laurenceb
mkdir bin # this creates /Users/laurenceb/bin

Export your bin directory to the PATH.

Open .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.

export PATH=$PATH:/Users/laurenceb/bin

After exporting the path export PATH=$PATH:/Users/laurenceb/bin, it’s best to reload the bash_profile using 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 ., press Command + SHIFT + .

Create a script file and make it executable

Go to your bin folder located in /Users/laurenceb.

cd bin
touch hello-world  # Create a file called hello-world 
# Open the file in your code editor of choice ex nano
nano hello-world
# and type on the very beginning of the file:
# followed by
echo Hello, World!

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”.

which bash

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.

chmod u+x hello-world

Now when you run the command, it will output the contents of the echo.

laurenceb@computer:~$ hello-world

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.

echo Hello, $who!

Note that 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.

echo Who are you?
read who
echo Hello, $who!


if statements use the if, then, else, and fi keywords. The condition goes in square brackets.

echo How old are you?
read age
if [ "$age" -gt 20 ]
    echo You can drink.
    echo You are too young to drink.

Operators are slightly different in bash.

Bash Operator Operator Description
-eq == Equal
-ne != Not equal
-gt > Greater than
-ge >= Greater than or equal
-lt < Less than
-le <= Less than or equal
-z ==null Is null


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.

for file in $FILES
  echo $(basename $file)

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!

git --version

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 or SSH. Choose 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.

git config --global “your username”
git config --global


git config --global
git config --global

Choose where you want to have the folder on your hard disk. I used my home directory.

cd ~            # go to the home folder
# This will clone the repo to your hard disk
git clone

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

read -r -p 'Commit message: ' desc  # prompt user for commit message
git add -A                          # track added deleted files
git commit -m "$desc"               # commit with message
git push origin master              # push to origin

The read command: -r do not allow backslashes to escape any characters -p prompt output the string PROMPT without a trailing newline before enter man read in terminal to see more!

Again you will need to give the correct permissions:

chmod u+x push_git

Restart the terminal with source .bash.profile or open a new window

source ~/.bash_profile 

Navigate in terminal to the local repo folder you cloned before and in that folder run the command push_git.
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!