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 learn what command line expansions,
Issued command is split into smaller tokens, tokens are then expanded or resolved. There are eight kinds of expansions performed. Those in order includes, Brace Expansion, Tilde Expansion, Shell parameter and variable Expansion, Command substitution, Arithmetic Expansion, Process substitution, Word splitting, File name Expansion. After the expansions, quote removal is performed.

Let's now learn, how these expansions work.

Brace Expansion!

Brace expansion is the shorthand for repetitive strings. Type
# echo file{4,5,6,7}
This will result in output, echo4, echo 5, echo 6 and echo 7. To avoid conflicts with parameter expansion, the string "${" is not considered eligible for brace expansion.

Tilde Expansion!

Tilde can refer to your home directory or some other user home directory. Tilde expansion feature is borrowed from C Shell. For example, ~/.bashrc, the .bashrc is looked upon under your home folder, however, ~tim/.bashrc, the bashrc file is looked under tim's home directory, you don't have to know path to home folder for tim.

Shell parameter and variable Expansion!

The "$" character is responsible for parameter expansion, command substitution, or arithmetic expansion. The basic form of parameter expansion is "${PARAMETER}". The value for PARAMETER is substituted during expansion Process. If the first character for ${PARAMETER} is exclamation, such as ${!PARAMETER}, then Bash uses the value of the variable formed from the rest of the parameter as the name of the variable.
Examples:
# echo $HOSTNAME
# echo ${!H*}


Command substitution!

Command substitution allows the output of the command to replace command itself. Use round brackets closure with dollar, or enclose command with in backticks.

Example:
# echo $(date)
# echo `date`
# echo "$HOSTNAME printed at `date`"

Arithmetic expansion!

Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. Format of an arithmatic expression is $(( EXPRESSION ))

Example:
NUMA=1
$((NUMA++))
echo NUMA

Process substitution!

Process substitution is a process where output of a command can be used as input to another command. Process substitution is supported on systems that support named pipes, First in first out or the /dev/fd method of naming open files.

Example:
# wc <(cat ~/.bash_profile)
Please note that no space is allowed between > or < and paranthesis.

Word splitting!
Word splitting occurs once any of the parameter, command or arithmetic expansions are done. The IFS variable holds the characters that Bash sees as word boundaries. Default boundary characters include space, tab and newline characters. If no expansion occurs, no splitting is performed.

Example
WS=hello world
WS="hello world"


File name expansion!
Bash scans each word for the characters "*", "?", and "[". If one of these characters appears, then the word is regarded as a PATTERN, and replaced with an alphabetically sorted list of file names matching the pattern.

Example:
# echo D*

This chapter covered a bit on expansions, In next chapter we will briefly touch echo and printf commands.