How to Remove Double Quotes in Bash


Bash is a popular Unix shell used for executing commands on a command line interface. Double quotes are often used in Bash for enclosing strings and variables. However, there may be situations where you need to remove double quotes from a string or variable in Bash. In this article, we will discuss various ways to remove double quotes in Bash and for demonstration I have used Linux Mint 21 operating system.

How to Remove Double Quotes in Bash

Removing double quotes in Bash can be useful for parsing JSON responses or performing operations on files with filenames containing double quotes. It makes it easier to manipulate strings or variables in a variety of situations, here are some ways to do it:

  • Using sed command
  • Using tr command
  • Using parameter expansion

Method 1: Using sed

The sed is a stream editor used for modifying files and text streams. It is used to remove double quotes from a string or variable in Bash. The syntax for using sed to remove double quotes is as follows:

sed 's/"//g'

Here for demonstration, I have given a simple bash code that remove the double quotes from a string:

#!/bin/bash

string1='"Linux Genie"'

string2=$(echo $string1 | sed 's/"//g')

echo "String with double qoutes: $string1"

echo "String without double qoutes: $string2"

In this example, the echo command is used to print the string “Linux Genie” to the standard output. The output of the echo command is then piped to the sed command. The sed command is used to substitute all occurrences of double quotes with an empty string. The g option is used to perform the substitution globally, i.e., all occurrences of double quotes are replaced.

Method 2: Using Tr

The tr is a command-line utility used for translating or deleting characters from a text stream. It can be used to remove double quotes from a string or variable in Bash. The syntax for using tr to remove double quotes is as follows:

tr -d '"'

Here for demonstration, I have given a simple bash code that remove the double quotes from a string:

#!/bin/bash

string1='"Linux Genie"'

string2=$(echo $string1 | tr -d '"')

echo "String with double qoutes: $string1"

echo "String without double qoutes: $string2"

In this example, the echo command is used to print the string “Linux Genie” to the standard output. The output of the echo command is then piped to the tr command. The tr command is used to delete all occurrences of double quotes.

Method 3: Using Parameter Expansion

Parameter expansion is a feature of Bash that allows you to manipulate variables. It can be used to remove double quotes from a variable in Bash. The syntax for using parameter expansion to remove double quotes is as follows:

\"/

Here for demonstration, I have given a simple bash code that remove the double quotes form a string:

#!/bin/bash

string1='"Linux Genie"'

string2=${string1//\"/}

echo "String with double quotes: $string1"

echo "String without double quotes: $string2"

In this example, a variable string is assigned the value “Hello, World!” enclosed in double quotes. The parameter expansion syntax ${string1//\”/} is used to replace all occurrences of double quotes in the variable string with an empty string. The resulting string is then printed to the standard output using the echo command.

Conclusion

In Bash, there are several ways to remove double quotes from a string or variable. The methods discussed in this article use sed, tr, and parameter expansion to remove double quotes. These methods are useful in various situations where you need to manipulate strings or variables in Bash.

Print Friendly, PDF & Email