When we are working to automate tasks it’s very much important to manipulate or work with strings. There are different types of manipulating strings using different string techniques. Below are some of the string manipulation techniques available for us.
Exact variable substitution
Advanced variable declaration
Indirect variable declaration
String length calculation
Sub Strings
String manipulation
Exact variable substitution in Shell Scripting
Example1: I have a variable VAR1 which is equal to abc but why I want to print along some more characters I am unable to print the variable data along appended data.
VAR1=abc echo "$VAR1xyz"
We will not get any output for above echo command. This is because your shell treats whatever characters followed after $ as a variable name. It stops variable name association only when it sees a space or “. But here our VAR1 is followed by some other charters “xyz” which make your shell to think VAR1xyz is a variable. To make sure Shell to understand VAR1 as a variable we have to bound the variable name with flower braces as shown below. Now try to execute echo command as below.
echo "${VAR1}xyz" Output: abcxyz Advanced Variable Declaration
Normally we can declare variables without much problem in Bash as it is independent of variable type. But if you want to check if the variable is defined or not it’s bit tedious job to check every time whether a variable is defined or not. For this, we can use advanced variable declaration techniques as shown below examples.
Example2: If VAR1 is set, use the set value, otherwise use surendra as value to VAR1. Note that this will not set VAR1 to surendra if VAR1 is not set but it will use for that occurrence.
For this example VAR1 and VAR2 values as follows.
VAR1=abc VAR2= echo ${VAR1:-surendra}
Output:
abc echo $VAR1 Output: abc echo ${VAR2:-surendra} Output: surendra echo $VAR2 No Output
So remember that :- will not set the variable VAR2 with surendra, but it uses for that instances.
Example3: If VAR1 is set, use already set value, otherwise set VAR1 value as surendra.
For this example VAR1 and VAR2 values as follows. VAR1=abc VAR2=
echo ${VAR1:=surendra} Output: abc
echo $VAR1 abc echo ${VAR2:=surendra} surendra echo $VAR2 surendra
Note: VAR2 is not set, but with this := option we set VAR2 permanently.
Example4: If VAR1 is set, use surendra. If it’s not set don’t use/set anything to it. In this case actual VAR1 value is not used if set.
For this example VAR1 and VAR2 values as follows. VAR1=abc VAR2=
echo ${VAR1:+surendra} surendra echo $VAR1 abc echo ${VAR2:+surendra} No Output echo $VAR2 No Output
Example5: If variable is not set throw some error stating that it’s not set.
echo ${VAR1?"This variable VAR1 is not set"} Output: This variable VAR1 is not set
Note: You can customize this error message.
Example6: Assign a variable with a number depending on a condition.
For example assign VAR1 with 67 if 45 is less then 35 else assign 89.
For this example VAR1 is not set
echo $VAR1
No output
Now try below example:
echo $((VAR1= 45<35 67:89=”” b=””>
Output:
89
echo $VAR1
89
Indirect variable declaration
Example7: How can we assign an variable to another value and get the value of first variable to second variable.Suppose take below example
VAR1=”Some data here”
VAR2=VAR1
How can we get VAR1 value by using VAR2? We can achieve this by using indirect variable declaration. To get VAR1 value in VAR2 us below code
echo ${!VAR2}
or
eval echo “$$VAR2”
Output:
Some data here
Note: Try to avoid assigning a variable to a variable.
String length calculation
Example8: How to get length of a string/variable. Use below command to get VAR1 length
For this example VAR1 is set to abcxyzabc
echo ${#VAR1}
Output
9
Sub Strings
Below examples deals with sub strings, ie getting part of a string.
Example9: Print VAR1 sub string from 4th character. This will print VAR1 value from 4th position till the end.
VAR1 value is set to abcxyzabc for following examples.
echo ${VAR1:3}
Output:
xyzabc
Example9: Printing characters from 3rd to 7th char. The below code will print VAR1 value from 3rd position till 5 chars from that position, so up to 7th.
echo ${VAR1:2:5}
Output:
cxyza
Example10: Print last 4 characters of a string. This will print last 4 characters of VAR1 value.
echo ${VAR1:(-4)}
or
echo ${VAR1: -4}
Output:
zabc
Example11: Print two chars which are in 4th and 3rd position from last.
echo ${VAR1: -4:2}
or
echo ${VAR1:(-4):2}
Output:
za
String manipulation
From next example on words we will see string manipulation. These are bit confusing and have to remember. If we can remember we can reduce the code when writing shell scripts.
Example12: Striping shortest sub string between a and b with in the string from LHS.
echo ${VAR1#a*c}
Output:
xyzabc
Example13: Striping longest sub string between a and b with in the string from LHS
echo ${VAR1##a*c}
Output:
no output
For below examples my string is VAR1=abcxyzabc
String manipulation for removing characters:
${VAR1%a*c} –Remove shortest values between a to c with in the string from RHS
output: abcxyz
${VAR1%%a*c} –Remove longest values between a to c with in the string from RHS
Output: Nothing will be there.
${VAR1#a*c} –Remove shortest values between a to b with in the string from LHS
output: xyzabc
${VAR1##a*c} –Remove longest values between a to b with in the string from LHS
Output: no output
${VAR1//xyz} –Remove all occurences of xyz
Output: abcabc
String manipulation for search and replace:
${VAR1/xyz/abc} –Replaces one occurrence of xyz with abc in VAR1 string
Output: abcabcabc
${VAR1//xyz/abc} –Replaces all occurrences of xyz with abc in VAR1 string
abcabcabc
${VAR1/#xyz/abc} –Replaces xyz if it’s present at start of VAR1
no change
${VAR1/%xyz/abc} –Replaces xyz if it’s present at end of VAR1
No change
String manipulation for upper case and lower case:
NEWVAR1=${VAR1^^} –Convert VAR1 value from lowercase to upper case
${VAR1^} –Convert VAR1 first value to upper case
${VAR1^^[aeiou]} –Convert only a,e,i,o,u chars to upper.
output: ABCXYZABC
NEWVAR1=${VAR1,,} –Convert VAR1 value from uppercase to lower case.
output: no change
${VAR1,} –Convert first character to lower
${VAR1,,[aeiou]} –Convert only a,e,i,o,u char to lower
${VAR1~} –Toggel first char
${VAR1~~} –Toggel all chars
${VAR1~~[aeiou]} –Toggel only a,e,i,o,u chars.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018