In: Computer Science
Escaping
At this point, there are four basic types of expansion the shell does for symbols:
Try it.
Try these examples of each type of expansion
$ echo $HOME # variable expansion $ echo $(whoami) # command substitution $ echo /etc/*.d # wildcard expansion $ echo ~leal # tilde expansion |
There are two types of escaping: partial quoting (double quotes) and full quoting (single quotes). Partial quoting escapes the content during some of these expansions, and full quoting escapes the content for all these.
Escaping |
||||
$variable expansion |
$(command) substitution |
*wildcard expansion |
~tilde expansion |
|
“partial quoting” |
X |
X |
||
‘full quoting’ |
X |
X |
X |
X |
Consider the following examples:
$ echo $(whoami) # normal command substitution $ echo “$(whoami)” # does not escape command substitution $ echo ‘$(whoami)’ # escapes the command substitution |
Loops
The bash for-loop has several variants. By default, it iterates over a list of values:
for i in 1 2 3 do echo “Hi $i” done |
We can also do this with a one-liner on the command line:
$ for i in 1 2 3; do echo “Hi $i”; done |
Bash can also iterate over a sequence:
for i in {1..3} do echo “Hi $i” done |
Bash can also iterate over a list resulting from an expansion:
for i in * do echo “Hi $i” done |
1. The basic form of variable expansion is ${variable}. The value of variable is substituted.. The braces are required when variable is a positional variable with more than one digit, or when variable is followed by a character that is not to be interpreted as part of its name.
2. Command substitution allows the output of a command to replace the command itself. Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.
3. Wildcards are useful in many ways for a GNU/Linux system and for various other uses. Commands can use wildcards to perform actions on more than one file at a time, or to find part of a phrase in a text file. There are many uses for wildcards, there are two different major ways that wildcards are used, they are globbing patterns/standard wildcards that are often used by the shell. The alternative is regular expressions, popular with many other commands and popular for use with text searching and manipulation.
4. The Bash shell provides some variables that are prefixed with ‘~’ known as Tilde Expansions.
Tilde expansion is the process of converting these abbreviations
to the directory names that they stand for.
Tilde expansion applies to the ‘~’ plus characters includes +, –
and N (which is integer) up to whitespace or a slash.The tilde
expansion is used to expand to several specific pathnames: