Home Bash scripting Bash Scripting – String Manipulation

Bash Scripting – String Manipulation

String manipulation in bash scripting

By Karthick
Published: Last Updated on 4.4K views

String manipulation is one of the fundamental concepts in bash scripting. In programming, strings are one of the data types which are an ordered sequence of characters. It is important that you know how to create and manipulate strings in bash. In this guide, we will learn string manipulation in Bash shell scripting with simple examples. You will be comfortable working with bash strings at the end of this article.

Variable assignment

Strings can be assigned to a variable and later used in the script for further processing. For example, I am creating a variable named "GREET_USER" and printing the string to the terminal.

$ GREET_USER="Hello, Thanks for visiting OSTechnix"
$ echo "$GREET_USER"
Assigning string to variable
Assigning string to variable

Bash has no strong type system so if you assign a value to a variable it will be treated as a string type. You can create strings with single, double, or no quotes.

There is a difference between single quotes and double quotes in bash. Single quotes prevent variable and command expansion while double quotes support it. Take a look at the below example.

I have created another variable named "SITE_NAME" and it is used in the "GREET_USER" variable. In double quotes, the variable will be expanded and in single quotes, the variable will not be expanded.

$ SITE_NAME="OSTechnix"
## DOUBLE QUOTES

$ GREET_USER="Hello, Thanks for visiting ${SITE_NAME}"
$ echo "$GREET_USER"
Usage of double quotes
Usage of double quotes
# SINGLE QUOTES

$ GREET_USER='Hello, Thanks for visiting ${SITE_NAME}'
$ echo "$GREET_USER"
Usage of single quotes
Usage of single quotes

Length of the string

To find the length of the string you can use the # symbol. Finding the length of the string will be useful in some cases where you have to write some logic based on the number of strings.

$ echo "${#SITE_NAME}"
Length of the string
Length of the string

Converting strings to Array

There are many ways to convert string data type to array type. The most simple way would be to enclose the string inside curly brackets.

$ ARR_TYPE=($GREET_USER)
$ echo ${ARR_TYPE[@]}
$ for element in ${ARR_TYPE[@]}; do
  echo $element
done
String to Array
String to Array

The second method would be to split the string and store it as an array based on the delimiter used in the string. In the previous example, space is used as the field separator (IFS) which is the default IFS in bash. For example, if you have a comma-separated string you can set the IFS to a comma and create an array. For more details about IFS, refer the following guide:

$ STR_TO_ARR="column1,column2,column3"
$ IFS=","
$ ARR=(${STR_TO_ARR})
$ for element in ${ARR[@]}; do echo $element; done
$ echo "${ARR[@]}"
Custom IFS - Array conversion
Custom IFS - Array conversion

Case conversion

Bash has built-in support for string case conversion. You have to use some special characters at the end of the string for case conversion like as shown below.

# SPECIAL CHARACTERS

,, ==> Converts an entire string to lowercase
^^ ==> Converts an entire string to Uppercase
~~ ==> Transpose Case
,  ==> Converts first letter alone to lowercase
^  ==> Converts first letter alone to uppercase
# ---- LOWER TO UPPER CASE ----
$ L_TO_U="welcome to ostechnix"
$ echo ${L_TO_U^^}


# ---- UPPER TO LOWER CASE ----
$ U_TO_L="WELCOME TO OSTECHNIX"
$ echo ${L_TO_U,,}


# ---- TRANSPOSE CASE ----
$ TRS_CASE="Welcome To OsTechnix"
$ echo ${TRS_CASE~~}


# ---- FIRST LETTER TO LOWERCASE ----
$ F_TO_L="OSTECHNIX"
$ echo ${F_TO_L,}


# ---- FIRST LETTER TO UPPERCASE ----
$ F_TO_U="ostechnix"
$ echo ${F_TO_U^}
Case conversion
Case conversion

You can also use regex pattern matching and convert the case for the matches.

$ L_TO_U="welcome to ostechnix"
$ echo ${L_TO_U^^[toe]}
Regex pattern matching - Case conversion
Regex pattern matching - Case conversion

String concatenation

You can concatenate multiple strings by placing the strings one after another. Depending upon how your strings are concatenated, you can add extra characters too.

$ SITE_NAME="OSTechnix"
$ MESSAGE="Welcome to"
$ echo "${MESSAGE} {SITE_NAME}"
String concatenation
String concatenation

String slicing

String slicing is a way of extracting a part of a string using the index position. Each character in the string is assigned an Integer value with which can be used to grab a portion of the string. Index value ranges from 0 to N. Below is the syntax for slicing.

{STRING:START:LENGTH}

START => Starting Index Position
LENGTH => Length of the String from position START

If LENGTH is not specified then the string will be printed till the end from the index position START.

$ SITE_NAME="OSTechnix"
$ echo ${SITE_NAME:2}
Slicing - Start to End
Slicing - Start to End

With LENGTH given, it will print the substring from the START index position and how many characters are to be printed.

$ echo ${SITE_NAME:2:2}
Slicing - Start and Length
Slicing - Start and Length

You can also reverse the string in many ways. The simplest way is to use the rev command. If you wish to do this in a bash way without using any external command then you have to write the logic manually.

$ echo ${SITE_NAME} | rev
Reverse string
Reverse string

Search and replace

There is a native way to search and replace characters in a string without using any external command like sed or awk.

To replace the first occurrence of substring, use the following syntax.

{STRING/X/Y}
The first occurrence of X will be replaced by Y.

Take a look at the below example where the first occurrence of the word "linux" is replaced with LINUX in uppercase.

$ MESSAGE="linux is awesome to work with.
Ubuntu is one of the powerful linux distribution"
$ echo $MESSAGE
$ echo ${MESSAGE/linux/LINUX}
Replace first occurrence
Replace first occurrence

To replace all the occurrences of the word, use the following syntax.

$ echo ${MESSAGE//linux/LINUX}
Replace all occurrences
Replace all occurrences

Remove substring

There are different ways to remove substring from the string. External utilities like sed, awk, or tr can be used or there is a way to do it in bash native way.

In the bash native way, parameter expansion is used to remove the substring. You have to use the % symbol followed by a pattern to remove. This will match the last found pattern and remove it.

For example, I wish to remove the words that come after the period (.) following syntax should be used. Here whatever comes after the period (.) will be removed. In this case, the last matched pattern .com is removed.

$ SITE_NAME="www.ostechnix.com"
$ echo ${SITE_NAME%.*}
Remove substring using % symbol
Remove substring using % symbol

To match the first occurrence of the pattern, use double percentage symbol

$ SITE_NAME="www.ostechnix.com"
$ echo ${SITE_NAME%%.*}
Remove substring using %% symbol
Remove substring using %% symbol

You can also use the # or ## symbol which will do a kind of inverse delete. With a single # symbol, the first pattern is matched and everything before the pattern is deleted.

$ SITE_NAME="www.ostechnix.com"
$ echo ${SITE_NAME#*.}
Remove substring using # symbol
Remove substring using # symbol

With the double ## symbol, the last pattern is matched and everything before the pattern is deleted.

$ SITE_NAME="www.ostechnix.com"
$ echo ${SITE_NAME##*.}
Remove substring using ## symbol
Remove substring using ## symbol

Conclusion

In this article, we have seen how to create strings in bash and different ways to manipulate the strings. To get familiar with bash string manipulation, launch the terminal and start practicing the examples provided in this guide. If you have any feedback or question, please use the comment section below.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More