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:

1
echo a{d,c,b}e    # an official example
img

Prints three similar words

Another example:

1
echo M{ike,ichelle,ichael}
img

Prints three persons' names

Tilde Expansion

We can get our home directory through the tilde symbol:

1
echo ~
img

Prints current user's home directory

We can also use the tilde symbol to see what files are in our home directory:

1
ll ~
img

Prints our home directory's files

Parameter Expansion

This expansion has difference presentations:

1
${param:[operator]word}
1
${[operator]param}
1
${param[operator]word}

If the param is null, use the word

The presentation of this expansion is as follows:

1
${param:-word}

Let’s demonstrate:

1
2
MY_NAME_HYPHEN='Dong'
echo My name is ${MY_NAME_HYPHEN:-DONG}
img

Prints the value of MY_NAME_HYPHEN because it's not null

If the param is null, replace with word and use

The presentation of this expansion is as follows:

1
${param:=word}

If there is a variable, use the value of the variable:

1
2
MY_NAME_EQUAL='Dong'
echo My name is ${MY_NAME_EQUAL:=DONG}
img

Prints the value of MY_NAME_EQUAL because it's not null

If there is no variable, use the word and assign the value to the variable:

1
2
3
echo $MY_NAME_EQUAL_NULL    # We haven't create this variable, so it's null
echo My name is ${MY_NAME_EQUAL_NULL:=DONG}    # := will assign DONG to MY_NAME_EQUAL_NULL variable because it's null
echo $MY_NAME_EQUAL_NULL    # Now it's been created
img

Prints DONG and assign it to MY_NAME_EQUAL_NULL variable

If the param is not null, use the word

The presentation of this expansion is as follows:

1
${param:+word}

If the variable exists, use the word:

1
2
MY_NAME_PLUS='Dong'
echo My name is ${MY_NAME_PLUS:+DONG}
img

Prints DONG because the MY_NAME_PLUS variable is not null

If the variable does not exist, nothing is used:

1
2
3
echo MY_NAME_PLUS_NULL    # We haven't create this variable, so it's null
echo My name is ${MY_NAME_PLUS_NULL:+DONG}    # Use nothing
echo MY_NAME_PLUS_NULL    # The MY_NAME_PLUS_NULL variable is null
img

Prints nothing because the MY_NAME_PLUS_NULL variable is not null

Use a value as a variable name

The presentation of this expansion is as follows:

1
${!param}

We can use the ! symbol to use the value of the variable as the name of another variable:

1
2
3
MY_FIRST_NAME="Dong"
MY_NAME='MY_FIRST_NAME'
echo My name is ${!MY_NAME}
img

Prints the MY_FIRST_NAME variable's value with indirection feature

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:

1
${param#*word}

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:

1
2
3
4
NAMES='Jack:Rose:Marry:John:Anna:Mike'
echo $NAMES
echo ${NAMES#*:}
echo $NAMES
img

Retrieves the variable's value before the first appearance removal and prints it after the deletion, this does not affect the variable's value.

Delete to the last appearance:

1
2
3
4
NAMES='Jack:Rose:Marry:John:Anna:Mike'
echo $NAMES
echo ${NAMES##*:}
echo $NAMES
img

Retrieves the variable's value before the last appearance removal and prints it after the deletion, this does not affect the variable's value.

References 3.5.1 Brace Expansion, 3.5.2 Tilde Expansion, 3.5.3 Shell Parameter Expansion

Buy me a coffeeBuy me a coffee