How do I pass on script arguments that contain quotes/spaces?

后端 未结 2 1902
野的像风
野的像风 2020-12-09 09:10

I\'m trying to write a script notify-finish that can be prepended to any command. When done, it will run the command given by the arguments following, then emai

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 09:35

    Use "$@" with quotes:

    prog="$1"
    "$@"
    ecode="$?"
    echo "$prog exited with $ecode"
    

    This will pass each argument exactly as it was received. If you don't include the quotes, each element will be split according to $IFS:

    • "$@" is like "$1" "$2" "$3" ..., passing each element as a separate argument.
    • "$*" is like "$1 $2 $3 ...", passing all elements concatenated as a single argument
    • $* and $@ is like $1 $2 $3 ..., breaking up each element on whitespace, expanding all globs, and passing each resulting word as a separate element ($IFS).

    The same is true for arrays, such as "${array[@]}" and "${array[*]}"

提交回复
热议问题