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.
Key Takeaways
- Learning about the methods of removing quotes from strings in Bash.
Free Downloads
4 Methods to Strip or Remove Quotes 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.
Method 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.
Case 1: Removing Double Quotes From String Using Parameter Expansion in Bash
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. However, follow the steps below to remove double quotes from a string in Bash:
Steps to Follow >
➊ Go to Ubuntu Terminal, open a script in the nano text editor by running the following command:
nano parameter.sh
- nano: A text editor.
- parameter.sh: This is a script. Here, I have named the script ‘parameter.sh’. You can name any of your choices.
➋ Now, write the following script inside the editor:
Script (parameter.sh) >
#!/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"
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.
➌ Then, press CTRL+S to save the file & press CTRL+X to exit.
➍ After that, use the command below to make the script executable:
chmod u+x parameter.sh
- chmod: Changes the permission of the files and directories.
- u+x: Adds the executable permission for the user.
- parameter.sh: The file which you want to make executable.
➎ Finally, run the script by the following command:
./parameter.sh
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: Removing Single Quotes From String Using Parameter Expansion in Bash
Use the syntax ‘${variable//pattern/}’ to successfully remove single quotes from a string where the pattern represents the single quotes (‘) in this case.
Script (parameter2.sh) >
#!/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"
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.
Now, run the script by the following command:
./parameter2.sh
From the above image, you can see that I have removed single quotes from the string ‘Hello, LinuxSimply!’ by using the parameter expansion.
Case 3: Removing Outermost Quotes From String Using Parameter Expansion in Bash
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 “.
Script (out.sh) >
#!/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"
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.
Now, run the script by the following command:
./out.sh
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”.
Method 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’s a basic overview of how you can use the ‘sed’ command to remove the single and double quotes from a string in Bash:
Case 1: Removing Double Quotes From String Using “sed” Command in Bash
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’.
Script (sed1.sh) >
#!/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"
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.
Now, run the script by the following command:
./sed1.sh
The above image dictates the removal of double quotes from a string “Hello, Softeko!” using the ‘sed’ command.
Case 2: Removing Single Quotes From String Using “sed” Command in Bash
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.
Script (sed.sh) >
#!/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"
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.
Now, run the script by the following command:
./sed.sh
The above image dictates the removal of single quotes from a string ‘Hello, Softeko!’ using the ‘sed’ command.
Method 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 this implementation:
Case 1: Removing Double Quotes From String Using “tr” Command in Bash
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:
Script (tr.sh) >
#!/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"
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.
Now, run the script by the following command:
./tr.sh
The above snapshot depicts that I have removed the double quotes from the string “Hello, Linuxsimply!” using the ‘tr’ command.
Case 2: Removing Single Quotes From String Using “tr” Command in Bash
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.
Script (tr2.sh) >
#!/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"
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.
Now, run the script by the following command:
./tr2.sh
The above snapshot depicts that I have removed the single quotes from the string ‘Hello, Linuxsimply!’ using the ‘tr’ command.
Method 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: Removing Double Quotes From String Using “awk” Command in Bash
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.
Script (awk.sh) >
#!/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"
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.
Now, run the script by the following command:
./awk.sh
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: Removing Single Quotes From String Using “awk” Command in Bash
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.
Script (awk2.sh) >
#!/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"
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.
Now, run the script by the following command:
./awk2.sh
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
Related Articles
- How to Escape Single Quotes in Bash? [3 Interactive Cases]
- How to Escape Double Quotes in Bash? [3 Easy Cases]
<< Go Back to Escape Quotes in Bash | Bash Quotes | Bash Scripting Tutorial