A beginner’s guide to bash scripting

A beginner’s guide to bash scripting

A Brief Introduction
Bash or Bourne again shell is a replacement to the original unix shell written by Stephen Bourne at Bell Labs.

It offers vast improvements over the original shell, which include
Integer arithmetic,
Indexed arrays,
Command line editing,
Unlimited command history.

Bash scripts are available by default on most Linux distributions.

To find out which version of bash you are running type the following command.

[leo@bash101 ~]$ bash --version
bash --version
GNU bash, version 4.2.45(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

In this bash tutorial you will learn how to write a simple bash script and then proceed to some intermediate bash scripting examples.

Getting Started with Bash scripting

Here’s a simple bash script which will tell you the disk usage in the current directory.

[leo@bash101 ~]$vim disk_usage
#Author Leo G

du -sh *

Assign execute permissions, using “chmod”.

[leo@bash101 ~]chmod +x disk_usage
[leo@bash101 ~] bash disk_usage
8.0K    File1
4.0K    disk_usage
4.0k    Directory

Explanation of the disk usage bash script

Let’s take each line of the script and understand how it work’s.

#!/bin/bash : This line is called shebang and it indicates that the bash interpreter should be used to run the script. The bash interpreter will execute commands in our script by searching for them in the directories which contain executables.

To find the path to your bash interpreter type

[leo@bash101 ~]which bash

To find which PATHS your interpreter will search in, type

[leo@bash101 ~]echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/s                                                                                        bin:/usr/local/rvm/bin:/home/leo_g/.local/bin:/home/leo_g/bin

#Author Leo G : “#” symbol indicates a comment. The exception to this is the first line “#!/bin/bash”.

du -sh * : This line as you might have guessed contains the disk usage command for the current directories.

That’s it, you can replace “du -sh *” with any command you like.

Debugging a bash script

Use “-x” to debug your bash scripts

[leo@linux-vps ~]$ bash -x disk_usage
+ du -sh File1 File2 Dire
8.0K    File1
4.0K    disk_usage
4.0k    Directory

I added a non existent bash command to the script, Now when you run the disk usage script with “-x” option you get:

[leolinux-vps ~]$ bash -x disk_usage
+ du -sh 1 disk_usage rails
8.0K    File1
4.0K    disk_usage
4.0k    Directory
+ nocommand
disk_usage: line 6: nocommand: command not found

Running multiple bash commands

You can run multiple commands by adding them on the next line

#Author Leo G

du -sh *
ls -l
echo " This script is done executing and you now know how to write a basic bash script"

You need to ensure that you add them on the next line, else you will need to use a semi colon after each command.

Bash Script to read user input

Before we go into user input, we need to learn about variables.

A small brief on Bash variables
Bash variables are used to store data and then perform an action on this data.
There are different types of variables, Local variables, Global variables, String variables,Integer variables,Constant variables, Array variables.
Global variables or environment variables are available in all shells while local variables are available only in the current shell.

Type “env” to see environment variables.

There are pre-defined bash variables as well.

Examples of Pre-defined Bash Variables
To find your user id

[leo@linux-vps ~]$ echo $UID

To find the path to your home directory

[leo_g@linux ~]$ echo $HOME

Here’s something you should know about variables
“Variables are case sensitive and capitalized by default. Giving local variables a lowercase name is a
convention which is sometimes applied. However, you are free to use the names you want or to mix cases.
Variables can also contain digits, but a name starting with a digit is not allowed”

[leo@linux-vps ~]$vim disk_usage
#Author Leo G

echo " Enter your directory: "
read x
du -sh "$x"

This script will prompt you for an input, enter the path to a file in the format “\path\file” and type enter.

In the above script the “read” command will store your input in the variable “x”. Since we need to find the disk usage of the value stored inside x and not of x, we need to prefix x with a dollar sign.

[leo@linux-vps ~]$bash disk_usage
 Enter your directory:
90M     /home/leo_g/

Note: I have enclosed “x” in double quotes so that the script does not throw an error when a file name has a space.

Let’s take another practical Bash script example

Backup Bash Script.

In this example we will take a daily backup of our files and notify the user when the backup was successful.

[leo@linux-vps ~]$vim backup
#Author Leo G

rsync -avz  -e "ssh " /path/to/yourfile user@backupserver.com:/backup/

echo "backup for $(date) "| mail -s "backup complete" user@youremail.com

When this script will run, “rsync” will sync files to a remote backup server and then send an email to us.

Running scripts daily with cronjob

[leo@linux-vps ~]$crontab -e

00 1 * * * /path/to/backup

Note: There is no error check in the script and I know a common question that may have come to your mind, What if rsync fails?. Well we will tackle that issue with error codes in my next post.

I like to keep posts simple, with less content and more practical examples, if you have any suggestions feel free to comment.

Here are some other Bash Scripts and Guides you may find useful

Bash Real time file syncing daemon with inotify tools



Creating Init/Systemd Scripts


Building an RPM on CentOS


Bash If statements, Exit Status and Comparison Operators


For a list of Guides and Tutorials see https://github.com/Leo-G/DevopsWiki

source (http://www.tldp.org/LDP/Bash-Beginners-Guide/Bash-Beginners-Guide.pdf)

Follow me

Leo G

Is a Linux Hobbyist and Enthusiast. He Strongly believes in OpenSource Software and would like you to view and download his software at https://github.com/Leo-g
Follow me

  • Pingback: Bash If statements, Exit Status and Comparison Operators, A beginners guide to bash scripting part 2 »()

  • laud

    please need help on this Linux project
    You,as a software engineer, have been asked to create an automated sales order modules for David using shell script. You need to perform the following task.
    * create four product-wise files, Monitors, keyboards, Printers, and scanners to record the sales transaction of the respective product.
    *when a customer places an order, store the product-wise order details.
    * create a file prices that will contain the prices for the product of different companies in the following format
    Monitor: ##: ##: ##
    Keyboard: ##: ##: ##
    Printer: ##: ##: ##
    Scanner: ##: ##: ##
    * In the preceding file
    1. price mentioned in this file are in $ (dollar )
    2. the fields in the files, Prices are colon (:) separated
    * Each data file (Monitor, keyboards, Printers and scanners) contains the following information:
    1. order_ID (generated automatically
    2. order_Date
    3. quantity
    4. company(c1/c2/c3)
    5. delivered(Y/N)
    6. Total_Amount
    *in the preceding file:
    1. fields in all files are colon (:) separated.
    2. companies can be c1, c2, or c3
    3. delivered fields will contain Y/N value Y stands for Yes and N stands for No
    4. Total_Amount is a calculated filed, which will be calculated as
    Total_Amount =Quantity * price
    5. you need to extract the price for the respective product and company from the file, prices.
    * Implement the following search operations:
    1. Id-wise order details
    2. Product-wise order details
    3. Delivered orders
    4. undelivered orders
    5. orders placed in the previous month
    6 orders placed in the current month
    * Delete the orders that have been delivered from the respective file. Orders that were placed in the previous month also nee to be deleted.
    * Generate invoice for the delivered orders in the format
    Pears computers
    current date : order date :
    order Id :
    product : quantity :
    company : unit price ($):
    total amount payable:
    * if an order is undelivered, do not generate the corresponding invoice. Rather display the message ” The order is not yet delivered, so invoice can not be generated .”
    * send all the invoice to the administrator (root user)
    take backup of each transaction file at the day end, for the security purpose.
    please help me

    • This should get you started https://gist.github.com/anonymous/e8ab3e666132164c5dc5

      It will fetch the price for a given product and company as well as calculate the total amount.

      The price file is space separated if you need : separated then you need to us the -F: with awk.

      You can use grep and sort to to do the search operations and the rest should not be too difficult to figure out

  • Pingback: Articles for 2014-Jul-1 | Readings for a day()

  • Pingback: A beginner’s guide to bash | 4an Nyheter()

  • Pingback: A beginner's guide to bash - Techbait Tech News()

  • Pingback: Links 9/12/2014: Greg Kroah-Hartman Interview, Fedora 21 Imminent | Techrights()

  • Pingback: A beginner’s guide to bash - mylinuxweb()

  • Istimsak Abdulbasir

    This tutorial was very helpful. I have been looking for scripting howtos that makes scripting not only easier to understand but also fun to learn.

    Because it was written in an easy to understand language, I was able to read through the entire article, without strain, learning important scripting techniques. One of those techniques was accepting user input from the keyboard. Great find.

    I’m looking forward to reading more scripting blogs.

  • Pingback: A beginners Guide to Bash Scripting on Linux | Linux Press()

  • Pingback: A beginners Guide to Bash Scripting on Linux | Linux Admins()

  • Pingback: Links 25/9/2015: GNU/Linux in Indian Government, NeoKylin in China | Techrights()