Introduction to Bash Scripting
Chapters
If you are looking for a beginer's course for Bash scripting, This course is very well for you. I've covered some of the most important topics to get you started. Course contents include
- Welcome message [Free]
- What is Bash
- Command line shortcuts and tricks
- Getting started with Bash profiles
- Bash Expansions
- Command Redirection and Piping
- Using Echo and PrintF and how to preventing command expansion
- Understanding local and environment variables
- Bash script syntax
- A basic Bash script
- Working with numbers and operators
- Working with arrays
- Controlling output with awk, grep and cut commands. How to use basic bash color themes with your output.
- Working with here document and files
- Flow control statements and loops
- Introduction to functions
- Interacting with user
- Closing note
I hope that you will learn heaps from this course, please leave your feedback and improvement suggestions.
Author: Subject Coach
Added on: 23rd Jan 2015
Please get in touch with your teacher or tutor in case you have a question related to this lesson
None just yet!
We touched arithmetic expansions very briefly before, In this chapter, we will explore numbers and expressions with arithmetic and comparison operators.
Let's open numbers.sh file with nano. I will start my script with usual HashBang line that we learned in previous chapter. I will now initialize a variable called num with value 2. I will now use echo command to print value of num. I will now decrement value of num by one with operator --, I will now echo value of num again.
Save this script and exit the edit mode. Assign execute permission on our shell script, and run the shell script with ./ in front. You will see that it printed the expected result. Now let's use another operator. I will initialize another variable TMP, and Assign it with value of result of, num multiply by 3. This assignment also updates value of num itself. let's now print result of this operation. When I execute this script, I will get the expected result.
Going further, Let's feed back value of num plus 20 to num variable itself. ":" is necessary because otherwise Bash attempts to interpret "$((num = $num + 20))" as a command. Let's echo num.
Let's execute this script and see what happens. A value of 23 will be printed, which is what we expected. Now let's use - operator to subtract twenty two from num. We will Assign the result to a new variable T. add echo command to print value of T. Executing this script now will print one for the last operation.
I will now use division operation and echo result of T divided by 3. This statement will be printed as it is, why? because arithmetic expressions must be enclosed with double round brackets. Let's update my script to just do that. Ok! now our script printed zero as a result of last expression but we were expecting different result. to enable printing floating point results we will make some adjustments to our scripts to use BC command. BC, for basic calculator, is "an arbitrary precision calculator language". Let's save and exit the script and execute it once more. This time it will print correct result.
There are various operators supported by Bash, these are shown in this table. You should try to use them in your scripts, to get first hand experience for using them.
Next we will touch comparison operators. Bash has two types of comparison operators, first for the integers, and second for the strings. The first part of this table shows comparison operators valid for intergers and second part of the table shows comparison operators valid for strings.
I will open comparison.sh with nano. comparisons in bash are done within double square brackets, a feature borrowed from Korn shell. Let's add a couple of expressions, first, compare one with one with equality operator, second, compare 1 with 0. We will print result of the last expression, by using dollar sign followed by question mark.
Let's save our script and execute it. 0 represents success and 1 represents failure.
Now let's check if 2 is greater than 10 using > operator. When I run this script, it displays 0 for success, why? because the numbers are bring compared as strings. Lexically 2 is of higher value, when compared to 1 0. Now let's update operator from > to -gt and run our script again. Now let's check for null and not nulls. Let's declare a variable N without any value. To check if value is null of not null, we use -n and -z operators. -n checks if value is not null, and -z checks if value is null. Let's now save this script and execute it. result is what we expected.
There are many comparison operators support by bash as shown in this table. You should try to use them in your own scripts to see how well to use them. Let's me give you one more example, this time we will use logical AND. logical AND is represented by to ampersands. Let's utilize our not null checks we did above to form our logical AND express. result will be failure because for sucess, both conditions should be successful, but in our case -n produces failure.
As an exercise, you should try to use as many operators as possible and see what drives what result.
In next chapter, we will cover arrays.