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!
Variables are named values that you will use in your Bash scripts. Variables generally store data or command output temporarily or they can also persist. We set value of a variable by simply typing VARIABLE=VALUE, we can reference them as ${VARIABLE}.
Let's set a variable VAT, with value "Welcome to $HOSTNAME", we can check value of this variable by using printF or echo command. Variables can also store value of other commands,
Let's set another variable FDIRS and feed value of LS command to this variable. When we echo this variable, output returned by LS command is now printed on standard output.
Variables will give your Bash scripts a lot of flexibility. Consider that you want to backup contents of a directory every day. You would want to create a directory, under a nominated backup folder, that use system date as name of the new directory, where files will be copied.
Let's initialize a variable called DIR, with the path of the desitnation directory for backing up your files, followed by the new directory name, which will infact be the currect system date. Now let's use this variable to create a new directory using MKDir command. Using this variable to represent desitnation directory, let's now use CP command to copy over the files to this folder. Now we will use this variable in CD command to switch to the newly created directory. We are in! and contents got copied perfectly.
We can add all these lines to a Bash script and run it every night to run a backup to a new folder.
Environment Variables are Variables that are available to all child shells, we use export command to export a variable to Environment Variables. Let's export my NAME variable. Let's launch another shell and our variable $NAME will be available to us.
We can use SET command, to display the names and values of all shell variables. Out variable NAME will be here. We can use ENV command to display names and values of environment Variables. Now because our NAME Variable was exported, we will find it under environment Variables too.
In next chapter, we will learn Bash script syntax and will write a basic bash script.