Copyright

Bash Scripts: If Statements

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 particular conditional statements using If commands. If statements are used to determine if certain actions are to be taken. Several examples of conditional statements will be provided and explained.

Bash: If

Bash scripts are used on the Linux operating system. These scripts carry out one or more commands. They are very powerful, however, as they support a number of features of programing languages. One of the major key words supported by Bash is the conditional statement, or if statement; it is a major function of any programming language.

If statements are conditional statements. That is, they look at a certain expression and decide whether or not to perform certain tasks. Conditional statements are the building blocks of all programming and scripting languages.

To translate to standard language, for example: If it is cloudy today, then I will listen to Journey; otherwise I will listen to Meat Loaf. This is a very simple if/then/else statement: We will only listen to Journey if it's raining. All other types of weather will require us to listen to Meat Loaf.

Syntax

The basic structure is highlighted in the figure basic if statement.

Bash Basic if Statement
bash if basic

A condition is surrounded by brackets. Usually standard brackets are used (). Additional commands can be added if the condition is not met using the else keyword, and the elif keyword is used to add an additional condition if the first one is false. The else is always last.

Conditional statements: Examples

The music example mentioned previously can be displayed using bash scripts. The following examples start with the basic concept and build in complexity.

The figure if statement with an else, shows a conditional statement with the else keyword. It first checks to see if the weather is sunny and to play Journey if that condition is met. Otherwise, Meat Loaf is chosen.

if Statement with an else
bash basic if

Even in the most complex scripts and programs, you will see many conditional statements with only an if and one else. But what if we want to check for more than sunny weather? What if we want to check for snow, or sun, and then any other type of weather? For example, if it is sunny, we listen to Journey. Otherwise if it is snowing, we listen to Styx. In all other types of weather, we play Meat Loaf.

In bash scripting, we need to add another if statement after the first condition. However, the keyword here is elif, which can be translated to else if. The figure More Complex if Statement with elif highlights this additional keyword.

More Complex if Statement with elif
bash if elif

Best practices

There are some basic rules to follow when constructing conditional statements (if statements).

Spaces between brackets

Always add spaces between the brackets and the check. Bash will return an error and not process.

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