Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Nested quotes



On Tue, 24 Nov 2015 08:36:27 +0100
Dominik Vogt <vogt@xxxxxxxxxxxxxxxxxx> wrote:
> Maybe I'm just thick this morning, but how do you "nest" double
> quotes here:
> 
>   $ FILE="foo bar"
>   $ echo "$(readlink -f $FILE)"
>   readlink: extra operand `bar'
> 
> Obviously $FILE needs to be quoted too.

I'm guessing the difference between you and Simon is you have the
SH_WORD_SPLIT option set for some sort of sh-compatibility.  You've now
discovered why it's not set by default.

You either to turn the option off again or provided explicit quoting of
$FILE where it occurs as an argument to readlink.

To wokr out how to quote it, you need to know that the fact the readlink
command is in $(...) inside quotes can be ignored --- the contents of an
$(...) expression are parsed step by step, so the only thing that will
terminate it is that closing ")", which can't occur in a syntactically
valid fashion for any other reason.  (That wasn't fully true until
quite recently, but the details only involve odd cases like case
statements where unpaired closing parentheses occur, so you can ignore
this subtlety for simple expressions even with older versions of the
shell.)

So it's fine to do

  echo "$(readlink -f "$FILE")"

but you might want to consider

  unsetopt shwordsplit

particularly if other people are using your zsh scripts functions.

It's also possible to turn SH_WORD_SPLIT off for one substitution,

  echo "$(readline -f ${==FILE}"

but that's got no obvious advantage here over simply quoting it, and
it's not portable.  The only advantage I can see is it's explicit to a
seasoned zsh user what you're trying to do.

pws



Messages sorted by: Reverse Date, Date, Thread, Author