Differences between revisions 1 and 4 (spanning 3 versions)
Revision 1 as of 2007-05-02 23:41:55
Size: 1011
Editor: redondos
Comment:
Revision 4 as of 2007-11-30 03:05:06
Size: 1709
Editor: GreyCat
Comment: change internal links
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:

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
Line 15: Line 13:
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 [:BashFAQ#faq5:array] will.
Line 23: Line 21:
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. Often, 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 [:BashFAQ#faq40:FAQ #40].

Another reason people attempt to do this is because they want to {{{echo "I am going to run this command: $command"}}} before they actually run it. If that's all you want, then simply use the {{{set -x}}} command, or invoke your script with {{{#!/bin/bash -x}}} or {{{bash -x ./myscript}}}. Note that you can turn it off and back on inside the script with {{{set +x}}} and {{{set -x}}}.

It's worth noting that you ''cannot'' put a pipeline command into an array variable and then execute it using the {{{"${array[@]}"}}} technique. The only way to store a pipeline in a variable would be to add (carefully!) a layer of quotes if necessary, store it in a string variable, and then use {{{eval}}} or {{{sh}}} to run the variable. This is [:BashFAQ#faq48:not recommended], for security reasons.

Anchor(faq50)

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

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 [:BashFAQ#faq5:array] will.

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

Often, 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 [:BashFAQ#faq40:FAQ #40].

Another reason people attempt to do this is because they want to echo "I am going to run this command: $command" before they actually run it. If that's all you want, then simply use the set -x command, or invoke your script with #!/bin/bash -x or bash -x ./myscript. Note that you can turn it off and back on inside the script with set +x and set -x.

It's worth noting that you cannot put a pipeline command into an array variable and then execute it using the "${array[@]}" technique. The only way to store a pipeline in a variable would be to add (carefully!) a layer of quotes if necessary, store it in a string variable, and then use eval or sh to run the variable. This is [:BashFAQ#faq48:not recommended], for security reasons.

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