How do I learn how to get quoting right in bash?

后端 未结 3 643
不思量自难忘°
不思量自难忘° 2021-01-06 16:40

I\'m constantly confused by the rules for quoting and evaluating when I\'m writing bash scripts. I know some of the basics, like the difference between \'\' and \"\" and ``,

3条回答
  •  感情败类
    2021-01-06 17:09

    Bruce Barnett's UNIX Shell Quote Tutorial is awesome, and the Bash FAQ/pitfalls/word splitting articles have tons of useful tips. A short summary:

    Unquoted strings can contain most characters, but not all (like newlines) , and many of them (including space) will have to be escaped. Just don't use them - If you fall for the temptation you might find that someone who modified the script forgot to include quotes once they became necessary.

    Single quoted strings can contain most characters, including NUL and newlines, but not single quotes, so they are also useful only for simple values.

    Backticks are for commands. They should only be used if your shell does not support $(). Example:

    current_dir=`pwd` # BAD! Don't do this!
    

    That command is bad, because when the right hand side of an assignment is not quoted the shell performs word splitting on it. It often leads to hard-to-reproduce bugs, because whitespace is difficult to check visually. To quote commands you have to use double quotes:

    current_dir="$(pwd)" # OK, but loses newlines at EOF
    

    Newlines at EOF are especially tricky. You can add a single character and strip it by using for example

    # Works for some commands, but not pwd
    current_dirx="$(pwd; echo x)"
    current_dir="${current_dirx%x}"
    printf %s "$current_dir"
    

    , but there's an additional difficulty because some commands (like pwd) will add a newline at the end of their output anyway, so you might have to remove that as well:

    # Works for some commands, including pwd
    current_dirx="$(pwd; echo x)"
    current_dir="${current_dirx%$'\nx'}"
    printf %s "$current_dir"
    

    Double quotes can contain any character (Try echo -ne "\0" | wc -c), but note that variables can't contain the NUL character.

    ANSI-C quotes can contain any characters except NUL (Try echo -ne $'\0' | wc -c), and provides handy escape codes to make it easier to work with special characters:

    printf %s $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
    printf %q $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
    touch -- $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
    rm -- $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
    

提交回复
热议问题