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

后端 未结 2 1892
野的像风
野的像风 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:32

    Put double-quotes around your variable substitutions to keep them from being parsed (note that this applies to all variables: $@, $1, and $PROG). Also: don't put a $ before a variable name when assigning to it; use # for comments; and, on the last line, the single-quotes will prevent variables from being substituted at all.

    PROG="$1"
    shift
    # Run program below
    "$PROG" "$@"
    ECODE=$? # note: this will always be a number, so it doesn't have to be protected with double-quotes
    echo -e "Subject: $(hostname): $PROG finished\r\nTo: <$USER>\r\n\r\nExited with $ECODE\r\n' | sendmail "$USER"
    

提交回复
热议问题