Tania Rascia

Skip Navigation
How to Create and Use Bash Scripts

How to Create and Use Bash Scripts

 /  31 responses

Bash scripting is an extremely useful and powerful part of system administration and development. It might seem extremely scary the first time you do it, but hopefully this guide will help ease the fear.

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.

Bash is available by default on Linux and macOS operating systems.

This is not meant to be an extensive guide to bash scripting, but just a straightforward guide to getting started with making your first script, and learning some basic bash syntax.


  • A basic command line knowledge is required. Everything you need to know to get started can be found in my How to Use the Command Line article.

This guide is for macOS. I’ll be using /Users/tania for all examples, but it will be /Users/your_username for you.


In this tutorial, we’re going to:

  • Create a bash script that can be run from any directory on the computer.
  • Learn about variables, conditions, looping, and user input with bash.
  • Create a simple Git deployment script.

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

You can make sure you’re in the main user directory by navigating to ~ (which is a shortcut for current user home directory, or /Users/tania). This will also be the default directory Terminal always opens in. Typing pwd will confirm the location, as well.

Create bin in that folder, or wherever you want your bash scripts to live.

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

2. Export your bin directory to the PATH

Open .bash_profile, which will be located at /Users/tania/.bash_profile, and add this line to the file. If .bash_profile doesn’t exist, create it.

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

If you don’t see hidden files and directories, or those that begin with a ., press Command + SHIFT + ..

3. Create a script file and make it executable

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

cd bin

Create a file called hello-world (no extension) in this folder.

touch hello-world

Open the file in your text editor of choice and type the following.


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”. You can confirm where the bash interpreter is located with which bash.

which bash

As is tradition, we’ll make a “Hello, World!” example to get this working.


echo Hello, World!

Now, you can try to run the file in the terminal.


But it won’t work.

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

[email protected]:~$ hello-world Hello, World!

Congrats, 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.

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.


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!
[email protected]:~$ hello-world Hello, World!

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!
[email protected]:~$ hello-world Who are you? Tania Hello, Tania!


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.
[email protected]:~$ check-id How old are you? 28 You can drink.

Operators are slightly different in bash than what you might be used to.

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

As I mentioned previously, 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 git-deploy command.


read -r -p 'Commit message: ' desc  # prompt user for commit message
git add .                           # track all files 
git add -u                          # track deletes
git commit -m "$desc"               # commit with message
git push origin master              # push to origin

If you’ve never used Git, check out Getting Started with Git for a primer.

Then just run the command.

[email protected]:$ git-deploy Commit message: Making some vague updates [master 0b0caaa] Making some vague updates 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 file.js create mode 100644 file2.js Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (5/5), 823 bytes | 823.00 KiB/s, done. Total 5 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), completed with 2 local objects. To https://github.com/me/repo.git 79f061b..0b0caaa master -> master


I hope this article has been helpful for you to get started with bash scripting. The concept of having a script that has complete access to anything on my computer was initially a frightening thought for me, but once I got accustomed to it I learned how useful and efficient it can be.


Get updated when I create new content.
Unsubscribe whenever. Never any spam.


I'm Tania. I turn down every ad, affiliate, and sponsor request I get. I write free resources that help thousands of people successfully become devs. If you enjoy my content, please consider supporting what I do.

Support my work

Write a response

Your email address will not be published. Required fields are marked *

All code will be displayed literally.


  • Joe S. says:

    Thank you.

  • tes says:

    Thanks a lot, great article!!

  • Mark says:

    This will fit nicely in my mind container.

  • Stefanie Fluin says:

    This is awesome, so cool! I love how easy this is to follow along – thumbs up!

  • laurence says:

    wow that was exactly what I was looking for.. just started using GitHub and got tired of typing the same commands over and over again. So well explained! Not only I got a script now but I understood a lot more about terminal and bin folders.

  • Hara says:

    Thanks Tania! Great content for a rookie like me 🙂

  • Duncan M Wallace says:

    This was clean and unobfuscated. Well done Dallas (sorry about the Red October quote)!!

  • Sarah says:

    This was super straightforward and helpful! Thanks so much!!!

  • guanyunfeng says:

    hello ,
    What does "-r" and "-p" int the "read -r -p 'Commit message: ' desc " mean?


  • Jessie says:

    Hi Tania –

    I have so many questions. For quick feedback, I had trouble running the script hello-world after modifying the read/write permissions (chmod stuff). As someone mentioned above refreshing it or force quitting out of terminal and reloading it did the trick. Also for those not sure how to modify your .bash_profile step #2. You can use an editor (VIM, VI, Nano), I used VIM. vim .bash_profile and added the "export PATH=$PATH:/Users/me:)/bi"

    1) What is it about bin, why do we call it bin, i've seen this in other scripts. Seems like the industry has adopted this, I'm just curious is this the reason for naming it bin? Aside for it being short for bin to hold things.
    2)What does the $ in PATH do? I would like to know why editing our PATH in our .bash_profile does.

    Great article, this has to be my first bash script!!

  • Sean Parry says:

    Hi Tania,

    Having tried to use your 'How to use the Command line' artical. It may or may not work on a Mac. However it certainly does not appear to work on a Linux PC ! Can you advise further.

  • Mars says:

    This is excellent, thank you!

  • shrestha says:

    Hi Tania,

    Thank you for this excellent article.

    After exporting the path `export PATH=$PATH:/Users/tania/bin`, it's best to reload the `bash_profile` using `source ~/.bash_profile`. This will allow users to use the `hello_world` script right away without restarting the shell/terminal.

    • Naveen says:

      Yeah, just so your comment, I was struggling all the way, wondering why hello-world wouldn't work, although I kept trying over and over again… but then I just restarted the terminal , and Voila it works fine! A point worth mentioning

  • Sheetal Gupta says:

    Thanks for intro It's Great 🙂

  • Maria says:

    Should I be running the script from the /bin directory? I'm still getting -bash: hello-world: command not found even after chmod 🙁

    • Anonymous says:

      I used command
      chmod +x hello-world
      and it ran.

      Thank 🙂

    • shrestha says:

      After adding "export PATH=$PATH:/YOUR_PATH_TO_BIN/bin" to your bash_profile, you'll need to reload it using `source ~/.bash_profile`. This will allow you to use the `hello-world` right away. Otherwise, you'll need to restart your terminal/shell.

      You can always use add "./" before the file name (e.g. "./hello-world") to run the script from the current directory.

  • Paul says:

    Great tutorial! Thank you for this!

  • jon rostran says:

    I believe you can make the script a bit more portable with the $HOME environment variable and a !#/usr/bin/env bash shebang. Either way, I found it very useful tutorial. Was wondering how to setup so I can call my scripts from anywhere. Thank you!

  • Joshua Amoah Baah says:

    What is meaning of the -r and -p used in the git-deploy script.

  • raffaele says:

    Hi Tania,

    your articles are awesome!!!

    Amazing Job!

  • Andrey Goluber says:

    ___123___How to Create and Use Bash Scripts – Tania Rascia___123___

  • Akpan Promise says:

    Hi Tania,

    Thank you for your article on Bash Scripting.

    I have been using Linux for a couple of years and never for once thought I would need bash scripting, but as work compile and become monotonous due to incessant repetition of tasks, I feel this is necessary.

    Thank you so much again!