simonzack
simonzack

Reputation: 20928

Bash/ZSH preserve quotes

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 '*' .

Upvotes: 4

Views: 749

Answers (3)

Charles Duffy
Charles Duffy

Reputation: 295403

In bash, you can access $BASH_COMMAND to see the literal, pre-parsing command being executed. Thus, while you can't prevent the shell from parsing an argument list, you can see its pre-parsed state.

However -- this gives you only the entire argument; you need to do string-splitting yourself, if going this route. As such, I would describe this as an ill-advised course of action.

Upvotes: 1

chepner
chepner

Reputation: 531155

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"

Upvotes: 4

tripleee
tripleee

Reputation: 189387

No, there is no way to disable the shell's quote handling.

If you want this for your own convenience, reading arguments from a here document or similar might be acceptable.

But if you want your users to be able to write quotes at the shell prompt and have them preserved, there is no way to do that (short of writing your own shell).

Upvotes: 2

Related Questions