Variable

A variable is a symbolic name for information. In the bash, it can be declared with an equal sign =. Unlike other languages such as C and Java, a variable type is not needed.

VAR=28

To use/expand the variable, you can simply type its name used in declaration with a prefix $ . If you use the variable with joining a value, you need to wrap the variable explicitly with ${}

echo $VAR
28
❯ echo ${VAR}iable
28iable

Variables could reference any value. There is one thing you have to care. When you declare a variable to reference a string, the string could have white spaces. To store the string and reference it properly, it is necessary to wrap with double quotes "". If a string doesn’t have any white spaces, it is an optional.

VAR="A B C"echo $VAR
A B C

Pattern Removal

Once you declare a variable, there are several operations to modify a expanded value. A pattern removal operation is one of them. In here, a pattern is different from a regular expression. The pattern has special characters having the following meanings:

  • * Matches any string, including the null string.
  • ? Matches any single character.
  • […] Matches any one of the enclosed characters.

% is a pattern removal operator to remove a trailing string. If there is no matched string, it will return the original string.

REPLY="HEY! HELLO"echo ${REPLY%O}
HEY! HELL
❯ echo ${REPLY%?}
HEY! HELL
❯ echo ${REPLY%[oO]}
HEY! HELL
❯ echo ${REPLY%X}
HEY! HELLO

There are two pattern removal operators. One is % and the other is %%. % supports a shortest pattern matching and %% supports a longest pattern matching. Here are examples.

echo ${REPLY%L*}
HEY! HEL
❯ echo ${REPLY%%L*}
HEY! HE

# is a similar operator like %. The only difference is that it is to remove a beginning string. There are also similar two operators: # and ##

REPLY="HEY! HELLO"echo ${REPLY#H}
EY! HELLO
❯ echo ${REPLY#*H}
EY! HELLO
❯ echo ${REPLY##*H}
ELLO

With a pattern removal operation, it would be an easy task to replace an extension of a given filename.

FILENAME="music.avi"echo ${FILENAME} ${FILENAME%.*}.mp3
music.avi music.mp3
❯ mv ${FILENAME} ${FILENAME%.*}.mp3

Pattern Replacement

If you want to replace a string, you can utilize a pattern replacement operation using ${variable/pattern/string}. If a expanded string of variable contains a matched pattern, a matched part will be replaced with a input string.

FILENAME="music.avi"echo ${FILENAME} ${FILENAME/.avi/.mp3}
music.avi music.mp3

By default, only the first match is replaced. To replace every match, the pattern should begin with /.

REPLY="HEY! HELLO"echo ${REPLY/HE/__}
__Y! HELLO
❯ echo ${REPLY//HE/__}
__Y! __LLO

If pattern begins with %, it must match at the end of the expanded string of variable.

REPEAT="HELLO HELLO"echo ${REPEAT/HELLO/hello}
hello HELLO
❯ echo ${REPEAT/%HELLO/hello}
HELLO hello

If pattern begins with #, it must match at the beginning of the expanded string of variable. As you know, the original string will be returned if there is no matched pattern. In this example, REPEAT value doesn’t begin with HELLO so it returns the original string for the #HELLO pattern.

REPEAT="HEY! HELLO HELLO"echo ${REPEAT/HELLO/hello}
HEY! hello HELLO
❯ echo ${REPEAT/#HELLO/hello}
HEY! HELLO HELLO

To get more information, type man bash and see the ‘Parameter Expansion’ and ‘Pattern Matching’ sections.