Differences between revisions 1 and 2
Revision 1 as of 2007-05-02 23:41:55
Size: 1011
Editor: redondos
Comment:
Revision 2 as of 2007-05-03 14:00:21
Size: 1013
Editor: GreyCat
Comment: make array a cross-reference to #5
Deletions are marked like this. Additions are marked like this.
Line 15: Line 15:
What's needed is a way to maintain each word as a separate item, even if that word contains multiple spaces. Quotes won't do it, but an array will. What's needed is a way to maintain each word as a separate item, even if that word contains multiple spaces. Quotes won't do it, but an [#faq5 array] will.
Line 23: Line 23:
Usually, this question arises when someone is trying to use {{{dialog}}} to construct a menu on the fly. For an example of how to do this properly, see [#faq40 FAQ #40] above. Usually, this question arises when someone is trying to use {{{dialog}}} to construct a menu on the fly. For an example of how to do this properly, see [#faq40 FAQ #40].

Anchor(faq50)

I'm trying to construct a command dynamically, but I can't figure out how to deal with quoted multi-word arguments.

Too see what the shell is doing with quotes use the set -x command in the terminal or use #!/bin/bash -x in a script

Some people attempt to do things like this:

    # Non-working example
    args="-s 'The subject' $address"
    mail $args < $body

This fails because of word-splitting. When $args is evaluated, it becomes four words: 'The is the second word, and subject' is the third word.

What's needed is a way to maintain each word as a separate item, even if that word contains multiple spaces. Quotes won't do it, but an [#faq5 array] will.

    # Working example
    args=(-s "The subject" "$address")
    mail "${args[@]}" < $body

Usually, this question arises when someone is trying to use dialog to construct a menu on the fly. For an example of how to do this properly, see [#faq40 FAQ #40].

BashFAQ/050 (last edited 2024-04-15 23:48:57 by larryv)