30. Bash Shell - Expansions: brace, tilde, parameter
Contents
Linux provides us with some expansions for duplicate, print current user’s directory, and parameter operations.
Brace Expansion
We can use curly braces to output something similar to the screen:
|
|
Another example:
|
|
Tilde Expansion
We can get our home directory through the tilde symbol:
|
|
We can also use the tilde symbol to see what files are in our home directory:
|
|
Parameter Expansion
This expansion has difference presentations:
|
|
|
|
|
|
If the param is null, use the word
The presentation of this expansion is as follows:
|
|
Let’s demonstrate:
|
|
If the param is null, replace with word and use
The presentation of this expansion is as follows:
|
|
If there is a variable, use the value of the variable:
|
|
If there is no variable, use the word and assign the value to the variable:
|
|
If the param is not null, use the word
The presentation of this expansion is as follows:
|
|
If the variable exists, use the word:
|
|
If the variable does not exist, nothing is used:
|
|
Use a value as a variable name
The presentation of this expansion is as follows:
|
|
We can use the ! symbol to use the value of the variable as the name of another variable:
|
|
Delete the content before the first or the last appearance of a specified word and output the remaining content
The presentation of this expansion is as follows:
|
|
We can use the # symbol to delete the content before the specified word’s first or last appearance include the word itself, but this won’t change the value of the variable.
Delete to the first appearance:
|
|
Delete to the last appearance:
|
|
References 3.5.1 Brace Expansion, 3.5.2 Tilde Expansion, 3.5.3 Shell Parameter Expansion
Author Dong Chen
LastMod Sun Mar 3 2019