1. Shell expansions
-
Wildcard expansions:
echo this is a test
echo
displays all the arguments that are passed to it.cd /usr
ls
echo *
The "
*
" character means match any characters in a filename. The shell expands the "*
" before executing the commandecho
.echo lib*
echo *bin
echo lib*32
echo */share
echo /*/*/bin
A question mark "
?
" matches any single character:echo lib??
echo lib???
Character sets are enclosed in square brackets:
echo lib[123456789]?
echo lib[xyz][123456789]?
Character ranges:
echo lib[1-9][1-9]
echo lib[a-z][1-9][1-9]
echo lib[1-9][!2]
Character classes:
echo lib[[:digit:]][[:digit:]]
echo lib[[:alpha:]][[:digit:]][[:digit:]]
echo lib[[:alnum:]][[:alnum:]][[:alnum:]]
echo lib[![:digit:]]*
echo lib[36[:lower:]]*
echo /etc/[[:upper:]]*
-
The tilde character ("
~
") expands to the home directory:echo ~
echo ~root
-
Arithmetic expansion:
echo $((2 + 2))
echo $(($((5**2)) * 3))
echo $(((5**2) * 3))
echo Five divided by two equals $((5/2))
echo with $((5%2)) left over
-
Brace expansion:
echo Front-{A,B,C}-Back
echo Number_{1..5}
echo {01..15}
echo {001..15}
echo {Z..A}
echo a{A{1,2},B{3,4}}b
cd
mkdir Photos
cd Photos
mkdir {2017..2019}-{01..12}
ls
-
Variable expansion:
echo $USER
printenv | less
echo $SUER
When the variable does not exist, it is expanded to the empty string.
-
Command substitution:
echo $(ls)
ls -l $(which cp)
echo "$(cal)"
Download lesson05/part1.cast
Loading asciinema cast...