Bash Script Examples

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

This lesson will cover Bash scripting in the bash shell. The Bash shell is the standard for Linux operating systems. Sample scripts are provided; introductory tasks as well as some more complicated examples are explained in detail.

Bash Scripts

Contrary to its name, Bash is a scripting language that does not involve large hammers or frustrated programmers.

Bash refers both to the scripting language and the shell in Linux. The shell program in Linux/Unix is a no-frills interface. If you have seen or worked with the Command program in Windows, it is very similar:

Bash Shell Image
batch screenshot

Commands are entered at the command line, and these commands can be entered directly into the command line or run through a shell script or program. The shell carries out those commands in order.

The bash shell, or Bourne Again shell, is a standard shell on Linux operating systems and is one of the easier shells to learn. Therefore, scripts written for the bash shell are called bash scripts.

Bash is a shell script built for Unix/Linux operating systems. A shell is basically a file that consists of a few Unix-based commands; it's labeled script because it contains a series of commands for the computer to perform. The shell is the program within Unix/Linux that interprets the commands and carries them out. Often the script is created for a set of repetitive tasks.

Bash Script Examples

All bash scripts must start with #!/bin/bash. The hashtag/exclamation point combo is called the shebang, which is quite fitting for a script named Bash. Following the shebang, you indicate the path that should be run. For Bash, it's always Bash. Other scripts may be different. The shebang is required on the first line of every file!

A basic screen output

The following code executes a simple output to the screen.

Simple Bash Sample
bash simple

Here is what the code does:

  • Line 1 is the shebang
  • Line 2 is a comment. It is for reference and documentation. It is highly recommended that code be commented, no matter how simple you think it is. In two years, you will go back and wonder what the heck you were doing. Comments help ease future frustration.
  • Line 3 is the actual command to show a message on the screen. The command echo prints the text after it. This command could be typed right into the command line and still run.

Adding variables

Let's add a little more programming to our simple output statement. This time the text will be saved to a variable so that it can be manipulated later.

(Don't forget the shebang!)

Bash Script with a Variable
bash simple variable

Adding user input

Let's take our example a step or two further. Since we made the output text into a variable, we can now take an input and let the user decide what to print. This also makes use of a delivered variable called $REPLY$.

Additionally, we will be asking for multiple inputs. The last input stores the values into an list, or array.

Bash Script for User Input
bash user input

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account