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}
41
In 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 Dog
In 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 Dog
In 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 Dog
and produces the result:
Quick Brown Fox Jumps Over The Lazy Dog
In 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 Dog
and produces the result:
Dog
In 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 Dog
and produces the result:
A Quick Brown Fox Jumps Over The Lazy
In 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 Dog
and produces the result:
A
In 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 DOG
In 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 dog
In 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 Dog
To 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/dash
string="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_IFS
echo "$variable_1" # value1
echo "$variable_2" # value2
echo "$variable_3" # value3
echo "$variable_4" # value4
In 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.