4 Ways to Remove or Strip Quotes From String in Bash With Cases

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

In Bash scripting, text manipulation has become a common task to perform using different utilities and methods. Removing quotes is such a useful task when you need to manipulate the strings except for the encasing quotes. So, read the following article carefully to clearly understand the context and learn how to remove quotes from a string in Bash.

4 Methods to Remove Quotes from String in Bash

Though quoting is used to prevent unexpected errors within codes, there may be some unintended processing failures while working with quotes within strings in Bash. So, it becomes a must to strip or remove the quotes from strings as easily as possible. Here, I am introducing you to 4 easy methods that will help you strip both single and double quotes efficaciously from strings in a Bash script.

1. Using Parameter Expansion to Remove Quotes in Bash

Parameter expansion is a basic feature in Bash using which you can manipulate variables and strings in multiple ways by employing various substitutions. In Bash, whether the quotes are at the beginning or at the end or even within the string, you can utilize the parameter expansion in a concise format to remove both single and double quotes from strings.

Different cases might arise while removing quotes using parameter expansion in bash. Here are three different cases described below:

Case 1: Remove Double Quotes from String

Use the syntax ‘${variable//pattern/}’ to remove double quotes (“) from a string by utilizing parameter expansion. Here, the pattern specifies the quote you want to remove.

Follow the script below to remove double quotes from a string in Bash:

#!/bin/bash

#Declaring variable
main_string='"Hello, LinuxSimply!"'

#Removing double quotes using parameter expansion
updated_string=${main_string//\"/}

#Displaying the output
echo "Main string: $main_string"
echo "Updated string: $updated_string"
EXPLANATION

Here, in #!/bin/bash, ‘#!’ is called Shebang’ or ‘Hashbang. The script begins with a variable declaration named main_string. Then, it uses the parameter expansion to replace all occurrences for double quotes with an empty string and stores the result in updated_string. Finally, the script displays the outputs indicating the main string and updated string using the echo command.

Output of removing double quotes using parameter expansion

From the image, you can see that I have removed the double quotes from the string “Hello, LinuxSimply!” by using the parameter expansion.

Case 2: Remove Single Quotes from String

Use the syntax ‘${variable//pattern/}’ to remove single quotes from a string where the pattern represents the single quotes (‘) in this case.

To remove single quotes from a string using parameter expansion in bash, use the following script:

#!/bin/bash

#Declaring variable
main_string="'Hello, LinuxSimply!'"

#Removing single quotes using parameter expansion
updated_string=${main_string//\'/}

#Displaying the output
echo "Main string: $main_string"
echo "Updated string: $updated_string"
EXPLANATION

First, the script declares a variable called main_string. Then, it uses the parameter expansion to replace all occurrences for single quotes with nothing and stores the result in the updated_string variable. Finally, the script terminates by displaying the outputs using the echo command.

Output of removing single quotes using parameter expansionFrom the above image, you can see that I have removed single quotes from the string Hello, LinuxSimply! by using the parameter expansion.

Case 3: Remove Outermost Quotes from String

In case of removing the outermost quotes (leading & trailing quotes) from a string, you need to use two types of syntaxes. To remove the leading selection you can use the syntax ‘${variable#pattern}’ and to remove the trailing selection you can use the syntax ‘${variable%pattern}’. For single quotes, replace the pattern with and for double quotes, replace with .

To remove outermost quotes from string using parameter expansion in bash, see the script described below:

#!/bin/bash

#Declaring variable
main_string='"Welcome to "LinuxSimply" website!"'

#Removing outermost quotes
updated_string="${main_string#\"}"
updated_string="${updated_string%\"}"

#Displaying the output
echo "Main string: $main_string"
echo "Updated string: $updated_string"
EXPLANATION

The Bash script initiates with a variable declaration called main_string. Then, it uses parameter expansion with different syntaxes to remove the leading and trailing double quotes from the declared variable and stores the result in updated_string. Finally, the script uses the echo command to display all the outputs.

Output of removing the outermost quotes using parameter expansion

From the image, you can see that I have removed only the outermost quotes from the string “Welcome to “LinuxSimply” website!” and preserved the inner quoted part “LinuxSimply”.

2. Using “sed” Command to Remove Quotes in Bash

The ‘sed (stream editor)’ command in Bash is an essential tool for text stream processing that allows you to manipulate various types of text operations. You can use this command to insert, delete, search, and replace specific patterns within a string or file. Here are some cases described below on how you can use the ‘sed’ command to remove the single and double quotes from a string in Bash:

Case 1: Remove Double Quotes from String

While removing double quotes from a string, you can easily use the “sed command” that searches for the specific patterns i.e. double quotes within the string that need to be removed and replaces them with an empty string. And to do so, the syntax you’ll use is ‘s/specific_pattern/replacement/g’. See the script given below to understand fully:

#!/bin/bash

#Declaring variable
main_string='"Hello, Softeko!"'

#Removing double quotes using the ‘sed’ command
updated_string=$(echo "$main_string" | sed 's/"//g')

#Displaying the output
echo "Main string: $main_string"
echo "Updated string: $updated_string"
EXPLANATION

The above script declares the variable main_string. It uses a pipe operator (|) to pass the output of the content of the defined variable to the ‘sed’ command and stores the result in the updated_string variable. Here, the ‘sed’ command replaces all occurrences of double quotes with nothing where the optional flag ‘g’ symbolizes the global substitution. At last, the script displays all the outputs using the echo command.

Output of removing double quotes using the 'sed' command

The above image dictates the removal of double quotes from a string “Hello, Softeko!” using the “sed command”.

Case 2: Remove Single Quotes from String

You can use the similar syntax ‘s/specific_pattern/replacement/g’ to remove single quotes from a string where the “sed command” will replace all occurrences of single quotes with an empty string.

To remove single quotes from a string using “sed command” in bash, use the following script:

#!/bin/bash

#Declaring variable
main_string="'Hello, Softeko!'"

#Removing single quotes using the ‘sed’ command
updated_string=$(echo "$main_string" | sed "s/'//g")

#Displaying the output
echo "Main string: $main_string"
echo "Updated string: $updated_string"
EXPLANATION

This script uses the ‘sed’ command to replace all occurrences of single quotes with an empty string where the ‘g’ flag symbolizes the global substitution. The pipe operator (|) passes the output of the content of the defined variable main_string to the ‘sed’ command and stores the result in the updated_string variable. Lastly, it displays the desired results using the echo command.

Output of removing single quotes using the 'sed' command

The above image dictates the removal of single quotes from a string ‘Hello, Softeko!’ using the ‘sed’ command.

3. Using “tr command” to Strip Quotes in Bash

The ‘tr’ command represents the character set translation or deletion from a string in Bash. Sometimes, you can use the command to replace and remove specific characters in a text stream. However, to remove the single and double quotes from a string, you need to use the syntax ‘tr -d [characters_set] [replacement]’ where the option ‘-d’ stands for ‘delete’. Below is a simple illustration of different relevant to this:

Case 1: Remove Double Quotes from String

When you specify double quotes as the set of characters that need to be removed, the ‘tr’ command replaces all occurrences of double quotes with an empty string. Here’s an example:

#!/bin/bash

#Declaring variable
main_string='"Hello, Linuxsimply!"'

#Removing double quotes using the ‘tr’ command
updated_string=$(echo "$main_string" | tr -d "'\"")

#Displaying the output
echo "Main string: $main_string"
echo "Updated string: $updated_string"
EXPLANATION

Here, the above script starts with a string stored in the main_string variable. The ‘tr’ command with the ‘-d’ option deletes all occurrences of double quotes. The pipe operator (|) used in the script passes the output of the content of the defined variable to the ‘tr’ command and stores it in updated_string. Finally, the script displays all results by availing the echo command.

Output of removing double quotes using the 'tr' command in bash

The above snapshot depicts that I have removed the double quotes from the string “Hello, Linuxsimply!” using the ‘tr’ command.

Case 2: Remove Single Quotes from String

As the ‘tr’ command works on a character-by-character basis, when you specify the single quotes, it replaces all occurrences of single quotes with an empty string.

To remove single quotes from a string using “tr command”, use the script given below:

#!/bin/bash

#Declaring variable
main_string="'Hello, Linuxsimply!'"

#Removing single quotes using the ‘tr’ command
updated_string=$(echo "$main_string" | tr -d "'")

#Displaying the output
echo "Main string: $main_string"
echo "Updated string: $updated_string"
EXPLANATION

This script performs command substitution where it uses the ‘tr’ command to delete all occurrences of single quotes. The pipe operator (|) used here actually passes the output of the content of the defined variable main_string to the ‘tr’ command. Afterward, the script echoes the outputs using the echo command.

Output of removing single quotes using the 'tr' command in bash

The above snapshot depicts that I have removed the single quotes from the string ‘Hello, Linuxsimply!’ using the ‘tr’ command.

4. Using “awk” Command to Remove Quotes in Bash

The ‘awk’ command is such a versatile tool that you can use it for data manipulation in various schemes including quote removal from strings in Bash. Generally, you can employ the ‘awk’ command to work in two ways while replacing or removing quotes. Either you can directly replace the quotes using the ‘gsub’ function or extract only the content without the quotes using the field separator (-F) option. The following section will give you a clear overview of this:

Case 1: Remove Double Quotes from String

When you want to directly replace the double quotes with an empty string, the ‘awk’ command uses the ‘gsub’ function to substitute the quotes globally as well as remove them effectively. Another way to remove double quotes is using the ‘-F’ option with the ‘awk’ command. This actually involves representing a custom field separator and printing the desired field without the double quotes.

Here’s an example to make the concept easier for you:

#!/bin/bash

#Declaring variables
string1='"Hello, Softeko!"'
string2='"Welcome to Softeko!"'

#Removing double quotes using the ‘awk’ command
#Using the ‘gsub’ function
updated_string1=$(echo "$string1" | awk '{gsub("\"", ""); print}')

#Using the field separator (-F) option
updated_string2=$(echo "$string2" | awk -F'"' '{print $2}')

#Displaying the output
echo "Main string: $string1"
echo "Updated string: $updated_string1"
echo "Main string: $string2"
echo "Updated string: $updated_string2"
EXPLANATION

The above script defines two variables string1 & string2. It then performs command substitution operations where the pipe operator (|) passes the output of the content of the defined variables to the ‘awk’ command. First, the script uses the ‘gsub’ function to substitute all occurrences of double quotes globally with an empty string and then prints the updated string into the updated_string1 variable.

Next, the script uses an option -F with the ‘awk’ command that sets the field separator to ‘double quotes’ where {print $2} tells to print the second field that is the content between the double quotes inside the string2 variable. Here, the result is stored in the updated_string2 variable. Finally, the script displays all the results using the echo command.

Output of removing double quotes using the 'awk' command in bash

The above image illustrates the deletion of double quotes from a string using the ‘awk’ command with the ‘gsub’ function and field separator option (-F).

Case 2: Remove Single Quotes from String

Similarly, you can remove single quotes using the ‘gsub’ function that globally replaces all occurrences of single quotes with an empty string and removes them. Additionally, you can use the -F (field separator) option to extract and print only the content inside the quotes, thus removing the single quotes. See the code given below:

#!/bin/bash

#Declaring variables
string1="'Hello, Softeko!'"
string2="'Welcome to Softeko!'"

#Removing single quotes using the ‘awk’ command
#Using the ‘gsub’ function
updated_string1=$(echo "$string1" | awk '{gsub("\047", ""); print}')

#Using the field separator (-F) option
updated_string2=$(echo "$string2" | awk -F"'" '{print $2}')

#Displaying the output
echo "Main string: $string1"
echo "Updated string: $updated_string1"
echo "Main string: $string2"
echo "Updated string: $updated_string2"
EXPLANATION

This script declares the variables string1 & string2. It then uses the pipe operator (|) to pass the output of the content of the defined variables to the “awk” Command. First, the script uses the ‘gsub’ function to substitute all occurrences of octal escape sequence\047’ of single quotes globally with an empty string and then prints the updated string into the updated_string1 variable.

Next, the script uses an option -F with the “awk” Command that sets the field separator to single quotes where ‘{print $2}’ tells to print the second field which is the content between the single quotes inside the string2 variable. The result is stored in the updated_string2 variable. Finally, the script employs the echo command to display all the results.

Output of removing single quotes using the 'awk' command in bash

The above image illustrates the deletion of single quotes from a string using the ‘awk’ command with the ‘gsub’ function and field separator option (-F).

Conclusion

To wrap up, you can remove quotes from a string in Bash using various methods. While going with an approach, just understand the context of your task, whether you want to remove single or double quotes from strings, and then go with the flow.

People Also Ask

Can I remove both leading/trailing whitespace and quotes from a string?

Yes, you can remove both leading/trailing whitespace and quotes from a string by employing the parameter expansion and ‘sed’ command.

Can I remove quotes from strings in a file and create a new file with the unquoted strings?

Surely, you can remove quotes from strings in a file and create a new file with the unquoted strings by appending the ‘sed’ command and file redirection.

Is it possible to remove quotes from a string using a custom delimiter except for single or double quotes?

Yes, it is possible to remove quotes from a string using a custom delimiter except for single or double quotes. For this, you can use the ‘sed’ command and mention your custom delimiter as the replacement.

Related Articles


<< Go Back to Escape Quotes in Bash | Bash Quotes | Bash Scripting Tutorial

Rate this post
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment