Linux Bash Scripting Tutorial
Use Double Quotes when line has multiple words.
$ echo "Hello World" $ echo " Hello World!" #in above 2 cases line has 2 words seperated by space. it echos "Hello World" on standard Output Device.(stdout) # Multi words should be sorrounded by Quotes(single or Double).
Below example assign a multi word value to a variable $city=new york 'new york' has 2 words, it should be included in Double Quotes. otherwise bash assumes 'york' is a shell command. so it is not, prints error message "bash: york command not found" correct syntax is: $ city="new york"
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
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 substitution allows the output of a command to replace the command itself.
command substitution syntax:
$(command) or `command`
command substitution example:
nirvana ~> echo $(cat countries.txt) nirvana ~> echo `cat countries.txt` nirvana ~> echo "$(cat countries.txt)" nirvana ~> echo "`cat countries.txt`"
Arthmetic Expansion allows shell to evaluate arithmetic expression. Please check Arithmetic Operations Section in Arithmetic Operations in Bash
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 matches zero or more characters
[ -> Sqaure Brackets for range of characters
? -> Question Mark matches 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
cond1.sh file2.sh
shopt shell options enable or disable shopt -P prints each shell option enabled/disabled -u disables option -s enables option ex: delete all files which starts with dot(.) rm .[a-zA-z0-9]* removes files which starts with single . rm .[.]*.* removes files which multiple dot in the beginning same as above using shopt shopt -s dotglob ; rm -f * ---------------- shopt -s nullglob text_files=("*.txt") #file name with spaces treats them as one text_files is an Array ------------------ shopt -s globstar • The new ** globbing operator matches filenames and directories recursively. for filename in **
ADS