Copyright

How to Write a Bash Script

Instructor: David Delony

David is a freelance writer specializing in technology. He holds a BA in communication.

In this lesson, you'll learn how to create simple Bash scripts, including how to set permissions, how to run scripts, and about if/then statements. This lesson covers the very basics of editing and running simple Bash scripts to automate tasks.

How to Write a Bash Script

Bash, or the Bourne Again Shell is not only the standard command line shell on Mac OS X and Linux, but it also serves as a useful programming language in its own right. Bash programmers create shell scripts to automate various system administration tasks, such as managing processes and users. Because you can use Linux and Unix commands you're already familiar with, it's very easy to get started programming with Bash.

Running Bash Scripts

In order to create a Bash script, you only need a text editor, such as Vi or Emacs. Don't use a word processor, such as Microsoft Word, because it includes special formatting characters that Bash won't recognize.

Whatever editor you use, you need to be able to tell the computer which shell you're using to run the script. In the very first line of your script, you should include this line:

#!/bin/bash

This is known as a shebang line because the slang terms for the '#' and '!' characters are called 'hash' and 'bang', respectively. If your version of bash is in a different directory, you'd change path to a different location.

When you're finished writing your script, you need to set the permissions of your script so that execute permissions are enabled.

To do this for a script called 'myscript' you'd just use the chmod command:

$ chmod +x myscript

Running your script is easy as well. You'd just use bash followed by the name of your script:

$ bash myscript

If your script is in a directory in your $PATH environment variable, you'd just type the name.

$ myscript

Variables

In any programming language, one of the most important concepts is the variable. A variable is simply a name that holds another value. For example, x might hold the value '2'.

Variables in bash work the same way. To set values, you use something like this:

variable=value

To set x to 2, you'd type this:

x=2

There must be no spaces between the equal sign in Bash. When setting variables, you don't use the $ sign, but when referring to variables in other parts of the script, be sure to precede it with a $, such as $x. It's best to give your variables meaningful names.

Here's an example of the classic 'Hello, world!' program to print the words 'Hello, world!' to the screen using the echo command.

#!/bin/bash

string='Hello, world!'

echo $string

If Statements

No programming language would be complete without some kind of flow control or ability to make decisions in a program. Bash has if statements much like other programming languages do.

To unlock this lesson you must be a Study.com 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 Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
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? Study.com 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
Support