Sometimes, we want to manipulate variables containing string like extracting sub-strings etc. For POSIX shell, we relies on both external program and some built-in functions to perform these manipulations.
sample="A Quick Brown Fox Jumps Over The Lazy Dog"We use the # indicator for our variable manipulation. This is a built-in function. 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 POSIX shell, search and replace function relies on external programs: echo and sed. The command is as follows:
$ variable="$(echo "$variable" | sed "s|Target|Replacement|g")"Therefore, for a example above, if we do the following with echo, we get:
$ sample="$(echo "$sample" | sed "s|Fox|Crow|g")"$ echo "$sample"A Quick Brown Crow Jumps Over The Lazy DogIn POSIX shell, search and delete function relies on external programs: echo and sed. The command is as follows:
$ variable="$(echo "$variable" | sed "s|Target||g")"Therefore, for a example above, if we do the following by deleting the fox, we get the following result:
$ sample="$(echo "$sample" | sed "s|Fox ||g")"$ echo "$sample"A Quick Brown Jumps Over The Lazy DogIn POSIX shell, we use the # variable manipulator. This is a built-in function. 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 DogIn POSIX shell, we use the double # variable manipulator. This is a built-in function. 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:
DogIn POSIX shell, we use the % variable manipulator. This is a built-in function. 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 LazyIn POSIX shell, we use the double % variable manipulator. This is a built-in function. 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 POSIX shell, to make the entire string into uppercase, we need the external commands echo and tr. We can proceed with:
echo "$sample" | tr '[:lower:]' '[:upper:]'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 POSIX shell, to make the entire string into uppercase, we need the external commands echo and tr. We can proceed with:
echo "$sample" | tr '[:upper:]' '[:lower:]'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 dogIn POSIX shell, 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 DogTo split string into multiple variable, we can use read to split a string based on the the $IFS delimiter, with the help of _EOF_ feed. Here's an example:
#!/bin/dashstring="value1|value2|value3|value4"old_IFS="$IFS"IFS='|' read -r variable_1 variable_2 variable_3 variable_4 <<-_EOF_$string_EOF_IFS="$old_IFS" && unset old_IFSecho "$variable_1" # value1echo "$variable_2" # value2echo "$variable_3" # value3echo "$variable_4" # value4In POSIX shell, we need echo and sed commands to perform whitespace trimming for all 3 types:
We will use the following string as an example:
string=" >-----Hello World >--"NOTE:
>----- denotes tab.To trim from left, we use the following command:
variable_1="$(echo "${string}" | sed -e 's/^[ \t]*//')"So if we use this command as a demo, we get:
$ echo "|$(echo "${string}" | sed -e 's/^[ \t]*//')|"|Hello World |To trim from right, we use the following command:
variable_2="$(echo "${string}" | sed -e 's/[ \t]*$//')"So if we use this command as a demo, we get:
$ echo "|$(echo "${string}" | sed -e 's/[ \t]*$//')|"| Hello World|To trim from both sides, we use the following command:
variable_3="$(echo "${string}" | sed -e 's/^[ \t]*//;s/[ \t]*$//')"So if we use this command as demo, we get:
echo "|$(echo "${string}" | sed -e 's/^[ \t]*//;s/[ \t]*$//')|"|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.