Sometimes, we want to manipulate variables containing string like extracting sub-strings etc. Instead of invoking external software to do such work, we can use the built-in string manipulation techniques. Consider our variables:
sample="A Quick Brown Fox Jumps Over The Lazy Dog"We use the # indicator for our variable manipulation. Here is an example:
${#variable}Therefore, for a example above, if we tries to print out variable length, it should produce the following result:
$ echo ${#sample}41In BASH, search and replace function is similar to the external program sed. However, it does not allow custom separator but strictly using /. The pattern is as follows:
${variable/Target/Replacement}Therefore, for a example above, if we do the following with echo, we get:
$ echo "${sample/Fox/Crow}"A Quick Brown Crow Jumps Over The Lazy DogYou are also allowed to use wildcard as well! Example:
${sample/F*x/Crow} ➔ A Quick Brown Crow Jumps Over The Lazy Dog${sample/F*/Crow} ➔ A Quick Brown Crow${sample/F??/Crow} ➔ A Quick Brown Crow Jumps Over The Lazy Dog${sample/F?/Crow} ➔ A Quick Brown Crowx Jumps Over The Lazy DogIn BASH, search and delete function is similar to the external program sed. However, it does not allow custom separator but strictly using /. Instead of providing a replacement, you simply don't provide anything. Here is the pattern:
${variable/Target}Therefore, for a example above, if we do the following by deleting the fox, we get the following result:
$ echo "${sample/Fox}"A Quick Brown Jumps Over The Lazy DogSimilarly, you are also allowed to use wildcard as well! Example:
${sample/F*x} ➔ A Quick Brown Jumps Over The Lazy Dog${sample/F*} ➔ A Quick Brown${sample/F??} ➔ A Quick Brown Jumps Over The Lazy Dog${sample/F?} ➔ A Quick Brown x Jumps Over The Lazy DogWe use the # variable manipulator. Say we want to remove the space " ", we can proceed with:
${sample#* }Here, we interpret it as remove everything (*) from the beginning until the first matching of space ( ). This will produce the following erasing pattern:
A Quick Brown Fox Jumps Over The Lazy Dogand produces the result:
Quick Brown Fox Jumps Over The Lazy DogWe use the double # variable manipulator. Say we want to remove the space " ", we can proceed with:
${sample##* }Here, we interpret it as remove everything (*) from the beginning until the last matching of space ( ). This will produce the following erasing pattern:
A Quick Brown Fox Jumps Over The Lazy Dogand produces the result:
DogWe use the % variable manipulator. Say we want to remove the space " ", we can proceed with:
${sample% *}Here, we interpret it as remove everything (*) from the end until the first matching of space ( ). This will produce the following erasing pattern:
A Quick Brown Fox Jumps Over The Lazy Dogand produces the result:
A Quick Brown Fox Jumps Over The LazyWe use the double % variable manipulator. Say we want to remove the space " ", we can proceed with:
${sample%% *}Here, we interpret it as remove everything (*) from the end until the last matching of space ( ). This will produce the following erasing pattern:
A Quick Brown Fox Jumps Over The Lazy Dogand produces the result:
AIn BASH, to make the entire string into uppercase, we use the built-in function: double cap. We can proceed with:
${sample^^}This translates the output by replacing all lowercase characters into the uppercase version. It produces the following results:
A QUICK BROWN FOX JUMPS OVER THE LAZY DOGIn BASH, to make the entire string into uppercase, we use the built-in function: double comma. We can proceed with:
${sample,,}This translates the output by replacing all uppercase characters into the lowercase version. It produces the following results:
a quick brown fox jumps over the lazy dogif the string is the word itself, you can use the built-in word lowercase first character function. Example, for this sample:
sample="Dog"The built-in function is:
${sample,}It produces the following result:
dogIn BASH, to make the entire string into Capitalize Words, we need the external commands echo and sed. We can proceed with:
echo "$sample" | sed -e "s/\b\(.\)/\u\1/g"This translates the output by replacing the first character of all words into the capitalize version. So, for a sample like:
sample="A quick Brown Fox jumps over the lazy dog"It produces the following results:
A Quick Brown Fox Jumps Over The Lazy Dogif the string is the word itself, you can use the built-in word capitalization function. Example, for this sample:
sample="james"The built-in function is:
${sample^}It produces the following result:
JamesTo split the string, we can use read to split a string based on the the $IFS delimiter, with the help of the built-in triple redirect indicator (<<<). Here's an example:
#!/bin/bashstring="value1|value2|value3|value4"old_IFS="$IFS"IFS='|' read -r variable_1 variable_2 variable_3 variable_4 <<< "$string"IFS="$old_IFS" && unset old_IFSecho "$variable_1"echo "$variable_2"echo "$variable_3"echo "$variable_4"Trimming whitespace has many ways: from the left, from the right, and from both left and right. We'll look into each of them respectively. In this case, we use the following example:
string=" >-----Hello World >--"NOTE:
>----- is a tab.To trim from left in BASH script, we use a compounded variable manipulators:
variable_1="${string#"${string%%[![:space:]]*}"}"So using the above with echo, we got:
$ echo "|${string#"${string%%[![:space:]]*}"}|"|Hello World |To trim from right in BASH script, we use a compounded variable manipulators:
variable_2="${string%"${string##*[![:space:]]}"}"So using the above with echo, we got:
echo "|${string%"${string##*[![:space:]]}"}|"| Hello World|The fastest way is to use read, the built-in function:
read -r variable_3 <<<"${string}"So using the above with echo, we got:
$ read -r variable_3 <<<"${string}"$ echo "|${variable_3}|"|Hello World|These are the variable manipulation techniques for you to apply and manipulate with. If you're ready, you can proceed to the next subsection.