Watching:  

Introduction to Bash Scripting


Author: Subject Coach
Added on: 23rd Jan 2015

 
Please note: You need to login to view this resource

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

  1. Welcome message [Free]
  2. What is Bash
  3. Command line shortcuts and tricks
  4. Getting started with Bash profiles
  5. Bash Expansions
  6. Command Redirection and Piping 
  7. Using Echo and PrintF and how to preventing command expansion
  8. Understanding local and environment variables
  9. Bash script syntax
  10. A basic Bash script
  11. Working with numbers and operators
  12. Working with arrays
  13. Controlling output with awk, grep and cut commands. How to use basic bash color themes with your output.
  14. Working with here document and files
  15. Flow control statements and loops
  16. Introduction to functions
  17. Interacting with user
  18. 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!

In this chapter, we will explore arrays. Arrays are important when you have to hold, more than one value at a time. Let's open file arrays.sh with nano. We will start with declaring an empty array which is represented by empty round brackets. Then we declare another array, myArr and initialize it with 4 values, which are red, blue, yellow and green. Point to note here is that there are no commas between different values of this array.

Arrays are zero indexed based, that means array indices start at 0. Thus to print first element of our array we will use zeroeth index. Indexes are provided within square brackets. To retrieve an element from an array, we use array variable name inside of curly braces, and square brackets to pass on the index for the value we are looking at, let's print values at zero and first index. You don't have to follow incremented values for array indexes, you can just use the index number to initialize array with a value for passed index.


To retrieve all elements within an array, we use array variable name inside of curly braces, then in square brackets, I will pass on at symbol. We can use the same index with appending closing square bracket with a colon, followed by minus one, to print last element of our array. Let's save this script and run it. We get the expected result.

To append another value to our array we will use plus equals operator. Value must be provided within round brackets or the passed value will be appended to first element of our array. So red will become red purple, Which is not what we want.

Bash supports associative arrays too. Given that you are using Bash version 4 or greater, to declare an associative array, we declare our array with dash A option. Then we start filling our array with out keys and values. Please note that if you key or value has a white space in it then, it must be enclosed in double quotes. To access a value for key, I will use normal syntax but rather providing the index, I will now provide key. Let's save this script and run it. Ok! so one value did not print. I may have made a mistake. Oh! The pen color key was wrong. Let's save this script again and run it. Now it displays the correct values.

In next chapter, We will touch some handy commands such as awk, grep and cut. We will also look at providing styling our output.