Contents /
Previous /
Next
Expansion
Expansion refers to the way a shell modifies
the command-line before executing it.
The bash shell performs:
-
Brace expansion, e.g., (file_{one,two,three}.txt
creates multiple files: file_one.txt, file_two.txt,
and file_three.txt)
-
~ : The full path of the users home directory
-
~+ : The current working directory
-
~- : The most recent previous working directory.
-
$ : Parameter (or variable) expansion
Parameter Expansion Manipulations:
${VAR :-default }
Use default if VAR is unset
${VAR :=default }
Same as previous and default is assigned to VAR.
${VAR :?message }
Display error message if VAR is unset
${VAR :offset } or ${VAR :n :l }
Produces the nth character of $VAR
and then the following l characters.
If l is not present, then all characters to
the right of the nth character are produced.
This is useful for splitting up strings.
Example:
TEXT=scripting_for_phun
echo ${TEXT:10:3}
echo ${TEXT:10}
${#VAR }
Gives the length of $VAR.
${!PRE *}
Gives a list of all variables whose
names begin with PRE.
${VAR #pattern }
The glob expression pattern removed
from the leading part of the string.
${VAR ##pattern }
The same as the previous expansion except
that pattern may contains wild cards.
${VAR %pattern }
The same as ${VAR #pattern } except that
characters are removed from the trailing
part of the string.
${VAR %%pattern }
The same as ${VAR ##pattern } except that
characters are removed from the trailing part.
${VAR /search /replace }
$VAR is returned with the first occurrence
of the string search replaced with replace.
${VAR /#search /replace }
Same as ${VAR /search /replace } except that the
match is attempted from the leading part of $VAR.
${VAR /%search /replace }
Same as ${VAR /search /replace } except that the
match is attempted at the trailing part of $VAR.
${VAR //search /replace }
Same as ${VAR /search /replace } except that all
instances of search are replaced.