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}
41
In 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 Dog
You 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 Dog
In 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 Dog
Similarly, 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 Dog
We 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 Dog
and produces the result:
Quick Brown Fox Jumps Over The Lazy Dog
We 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 Dog
and produces the result:
Dog
We 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 Dog
and produces the result:
A Quick Brown Fox Jumps Over The Lazy
We 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 Dog
and produces the result:
A
In 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 DOG
In 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 dog
if 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:
dog
In 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 Dog
if 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:
James
To 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/bash
string="value1|value2|value3|value4"
old_IFS="$IFS"
IFS='|' read -r variable_1 variable_2 variable_3 variable_4 <<< "$string"
IFS="$old_IFS" && unset old_IFS
echo "$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.