Académique Documents
Professionnel Documents
Culture Documents
Me Articles Contact
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.
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.
Prerequisites
• 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.
Goals
• 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.
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.
export PATH=$PATH:/Users/tania/bin
If you don't see hidden files and directories, or those that begin with a . , press Command +
SHIFT + .. If Terminal.app is open, quit and reopen it so the PATH gets updated.
cd bin
touch hello-world
Open the file in your text editor of choice and type the following.
hello‐world
#!/bin/bash
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
/bin/bash
hello‐world
#!/bin/bash
hello-world
•••
-bash: hello-world: command not found
Now when you run the command, it will output the contents of the echo .
•••
tania@computer:~$ 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.
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 .
hello‐world
#!/bin/bash
who="World"
•••
tania@computer:~$ hello-world
Hello, World!
Note that who = "World" is not valid ‐ there must not be a space between variable and value.
Reading
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.
hello‐world
#!/bin/bash
read who
•••
tania@computer:~$ hello-world
Who are you?
Tania
Hello, Tania!
Conditionals
if statements use the if , then , else , and fi keywords. The condition goes in square
brackets.
check‐id
#!/bin/bash
read age
if [ "$age" -gt 20 ]
then
echo You can drink.
else
echo You are too young to drink.
fi
•••
tania@computer:~$ check-id
How old are you?
28
You can drink.
Operators are slightly different in bash than what you might be used to.
-eq == Equal
-z == null Is null
Looping
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.
#!/bin/bash
FILES=/Users/tania/dev/*
git‐deploy
#!/bin/bash
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.
•••
tania@computer:$ 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
Conclusion
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.
About
Tania’s List
Open‐source projects and web development tutorials
Subscribe
I’m Tania Rascia, a web developer and writer. I document everything I learn in clear, concise
tutorials that help thousands of people start coding careers. My site has no ads, sponsors, or
bullshit.
3 comments
Sasa Milenkovic
44 minutes ago
waldo
19 hours ago
Danish
5 days ago
Its interesting and helpful, even i had tried to write bash for the very first time, i really need something like this. :﴿
Thanks
Ko‐Fi
Patreon
Newsletter
RSS