Linux/Bash: How to unquote?

13,942

Solution 1

Try xargs:

$ echo $x
'a b' 'c'

$ echo $x | xargs ./echo
argc = 3
argv[0] = ./echo
argv[1] = a b
argv[2] = c

Solution 2

eval echo $x

this will pass a b as first argument and c as the second one

Note: this will actually evaluate arguments, eg:

$ x='$((3+5))'
$ eval echo $x
8

If that's not what you want use @vanza's xargs.

Solution 3

Usually, if you're trying to store multiple "words" in a single variable, the recommended method isn't to use embedded quotes, but to use an array:

X=('a b' 'c')
printf "%s\n" "${X[@]}"

prints:

a b
c
Share:
13,942
Daniel Marschall
Author by

Daniel Marschall

My name is Daniel Marschall, I am born in 1987, and I have developed software since my youth. After receiving a certificate in 2005 at the Theodor Heuss Secondary School Heidelberg and the general higher education entrance qualification 2008 with focus on biotechnology at the Marie-Baum-Schule Heidelberg, I started studying computer science at the University of Mannheim, which I broke off in favor of an apprenticeship. In July 2015, I completed my training as a specialist in application development at the company RINNTECH e.K. in Heidelberg (measuring technology for trees and wooden construction). In 2016, I began working at HickelSOFT Huth GmbH (ERP software and POS the beverage trade) and became new owner and CEO of that company in 2021. In addition to the creation of websites and the development of free software in projects of various types, my particular interests include art and music.

Updated on June 04, 2022

Comments

  • Daniel Marschall
    Daniel Marschall about 2 years

    Following command

    echo 'a b' 'c'
    

    outputs

    a b c
    

    But the following

    X="'a b' 'c'"
    echo $X;
    

    will outout

    'a b' 'c'
    

    I am searching a way to unquote $X , so that it will output "a b c" , but without losing the merged 'a b' argument. (= 2 arguments instead of 3, makes no difference for command 'echo', but for other commands like 'cp')