Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»Bash – Remove Double Quote (“”) from a String

    Bash – Remove Double Quote (“”) from a String

    By RahulSeptember 25, 20221 Min Read

    This tutorial will help you to remove the start and ending double quotes from strings in a shell script. Where the string is stored in a variable.

    Advertisement

    Remove Double Quote from a String

    The sed command line utility helps to easily handle this. A single-line sed command can remove quotes from the start and end of the string.

    sed -e 's/^"//' -e 's/"$//' <<<"$var1" 
    

    The above sed command executes two expressions against the variable value.

    • The first expression 's/^"//' will remove the starting quote from the string.
    • Second expression 's/"$//' will remove the ending quote from the string.

    Shell Script Remove Double Quote from String

    Remove Double Quote and Store Output

    The result will be printed on the terminal. You can also save the result to a variable and or redirect output to a file.

    The below commands will help you to remove double quotes and store output to the same or different variable.

    var2=`sed -e 's/^"//' -e 's/"$//' <<<"$var1"`    #Save in another variable 
    var1=`sed -e 's/^"//' -e 's/"$//' <<<"$var1"`    #Save in same variable 
    

    Even you can store the result in a file. like:

    sed -e 's/^"//' -e 's/"$//' <<<"$var1" > out_var.txt 
    

    Conclusion

    This tutorial helped you to remove the start and ending double quotes from a string stored in a variable using shell script.

    bash string
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Implementing a Linux Server Security Audit: Best Practices and Tools

    15 Practical Examples of dd Command in Linux

    Iptables: Common Firewall Rules and Commands

    View 4 Comments

    4 Comments

    1. Jof8b4dh on August 7, 2021 11:12 am

      Re-sent due to typo 🙁
      (missing “$”)

      If you are using bash there is a far more efficient solution, without the need to invoke an external program (sed):
      var2=”{var1%\”}”; var2=”${var2#\”}”
      If you definitely know from the context that *both* double quotes are really present within var1, then there is an even more efficient solution:
      var2=”${var1:1:-1}”
      Not sure in case of other shells.

      Warning in case you copy+paste the command lines from this comment:
      The forum software seems to capriciously change ordinary double quotes into typographical ones!

      Reply
      • Jof8b4dh on August 7, 2021 11:37 am

        Heck, this wasn’t a typo!
        Dear Moderator,
        in fact, your forum software swallowed the missing “$” AGAIN !
        I’m afraid, your software is getting confused by the double quotes when trying to convert then to the allegedly “correct” typographical ones. (I’m hating such software highhandedness 😉 )
        Please add the “$” before the first left brace in the first command line in my comment.
        Thank you

        Reply
    2. Jof8b4dh on August 7, 2021 10:52 am

      If you are using bash there is a far more efficient solution, without the need to invoke an external program (sed):
      var2=”{var1%\”}”; var2=”${var2#\”}”
      If you definitely know from the context that *both* double quotes are really present within var1, then there is an even more efficient solution:
      var2=”${var1:1:-1}”
      Not sure in case of other shells.

      Reply
      • Wanderer on February 7, 2023 6:47 pm

        Or just {var1//\”}

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.