Get to know Linux: Bash scripting basics

Jack Wallen
May 20, 2009
Updated • Dec 22, 2012
Linux
|
3

After using Linux for a while you will eventually find yourself needing to create a bash script. And just what is a bash script? A bash script is a script that is run through the bash shell. Usually a bash script is a user-generated script that serves a specific purpose and combines numerous commands into one convenient script.

I am a big fan of bash scripting. I use them frequently to create backup scripts, scripts that are used via cron, and much more. But before I could write even a simple bash script, I had to understand the basics. And that's what you will learn here - the very basics of bash scripting. This will be a foundation you can build upon so that your bash scripts can get more and more complex.

The structure

A bash script consists of just a few pieces. First and foremost you have to actually create the file. This file will contain all of your scripting and will have to be made executable by the user. Once the file is complete and saved you will make this executable with the command:

chmod u+x FILENAME

Where FILENAME is the actual name of your file.

Now within the file you will have to at least have two minimal pieces:

  • Shell declaration
  • Script

The shell declaration is a statement that declares what shell you are to use. For nearly all of your Linux needs you will use the bash shell. To declare the bash shell being used your declaration will be:

#! /bin/bash

With that declaration all commands will be run through the bash shell.

The script is the contents of the shell script you will write. The script will most often consist of commands.

Hello world

Ah hello world! Who hasn't or used this as an example. Let's take a look at what an Hello World script wold look like. We'll add a few variations to highlight some of the subtle differences.

The basic Hello World! script would look like:

#! /bin/bash
echo "Hello World!"

Once you save it (we'll call it "hello") and make it executable you can run it by issuing the command:

~/hello

and you will see the output:

Hello World!

Now let's use variable declaration in this script. Using variables will make your scripting much more versatile.

#! /bin/bash
STRING1="Hello"
STRING2="World!"
echo $STRING1 $STRING2

Now let's modify this to use a global variable. One useful global variable is USER. At the bash prompt enter echo $USER and bash will return the username that is currently logged in. So change your hello world script to look like:

#! /bin/bash
STRING1="Hello"
echo $STRING1 $USER

When you run this script you will see:

Hello USER

Where USER is the actual username logged in. You can test this by su'ing to a different user (such as root) and running the script. If you are root (make sure you su to root with the command su - or you won't have root's prompt, only root's privileges) you will see:

Hello root

Final thoughts

And there you have the very fundamentals of bash scripting. Hopefully you can see how to build on the Hello World! example.

Advertisement

Previous Post: «
Next Post: «

Comments

  1. thebillywayne said on May 24, 2009 at 12:55 pm
    Reply

    Good intro to bash scripting. I hope you keep up this series and get into more advanced scripting.

  2. seminoob said on May 21, 2009 at 11:21 am
    Reply

    You might wanna use a different path in the hashbang:
    #!/usr/bin/env bash

  3. Zim said on May 21, 2009 at 6:42 am
    Reply

    Thanks for this! :)

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.