Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash/ZSH preserve quotes

Tags:

bash

shell

zsh

Is there a way to tell bash/zsh to not parse the quotes at all but give them to a shell function verbatim?

$ argtest abc def "ghi jkl" $'mno\tpqr' $'stu\nvwx'
abc
def
"ghi jkl"
$'mno\tpqr'
$'stu\nvwx'

You might be thinking why I don't just do

argtest abc def '"ghi jkl"' "$'mno\tpqr'" "$'stu\nvwx'"

But the argtest function I'm trying to create tries to wrap around other commands which can have noglob prefixes. So I need a way of being able to tell apart * and '*' .

like image 366
simonzack Avatar asked Oct 16 '25 01:10

simonzack


1 Answers

In zsh, you can use the q parameter expansion flag, but it's messy. One q escapes individual characters as necessary; two expands the text in single quotes; three in double quotes. (The notation ${:-stuff} simply expands to the text following :-; it's a wrapper that allows you to create anonymous parameters.)

$ echo "foo bar"
foo bar
$ echo ${(qq):-"foo bar"}
'foo bar'
$ echo ${(qqq):-"foo bar"}
"foo bar"

$ argtest () {
function>   echo "$1"
function> }
$ argtest "foo bar"
foo bar
$ argtest ${(qqq):-"foo bar"}
"foo bar"
like image 93
chepner Avatar answered Oct 18 '25 14:10

chepner