Linux Bash Scripting Tutorial

Shell Scripting Basics


Shell Expansion

Brace Expansion

Brace expansion is a technique to generate aribtrary strings.content within curly braces will be appended to preceding text,
range of characters or numbers specified with double dot notation ..

		nirvana ~> echo file{a,b,c}
		filea fileb filec
		nirvana ~> echo file{1,5,7}
		file1 file5 file7
		nirvana ~> echo file{1..3}
		file1 file2 file3
		nirvana ~> echo {a..z}.txt
		a.txt b.txt ... z.txt

Tilde Expansion

     Each word and shell variable on a commandline is checked to see whether it begins with unquoted tilde character. Tilde(~) by itself or followed by a / is replaced by the HOME variable

Command Substution

command substitution allows the output of a command to replace the command itself.
command substitution syntax:




command substitution example:

	nirvana ~> echo $(cat countries.txt)
	nirvana ~> echo `cat countries.txt`
	nirvana ~> echo "$(cat countries.txt)"
	nirvana ~> echo "`cat countries.txt`"

Arithmetic Expansion

     Arthmetic Expansion allows shell to evaluate arithmetic expression.

Process Expansion

File name Expansion

Whenever Shell sees wild characters in command line,File Name expansion takes place. These wild characters in String, also known as Pattern in regular expressions
Wild Characters

* -> Asterisk matachers zero or more characters
[ -> Sqaure Barackets for range of characters
? -> Question Mark mataches zero or one character
These characters also know as file name metacharacters.

echo * displays all files and directories in current directory
ls * displays all files in current directory + all sub directories files also.
cat course? displays files content named as course1,couse2 ...course9
cat course* displays files content, files name starting with name "course"

ls [0-9]* displays all files starting with numbers
ls [a-z]* displays all files starting with lower-case letters from a to z
ls [A-Z]* displays all files starting with upper-case letters from A to Z
ls [a-zA-Z]* displays all files starting with lower/upper case letters
ls [a–np–z]* displays all files starting letters a to n or q to z
ls *[0-9].sh displays all files starting with any character and ending with digit