如何将引号内的所有输入转化为变量

How to get all input inside quote into variable

我想将我在 " 中发送的任何内容插入到变量中。

例如:

check.sh:

#!/bin/bash
./b.sh -a "$@"

b.sh:

#!/bin/bash

while getopts ":a:b:c:" opt; do
  case ${opt} in
        a) A="$OPTARG"
;;
        b) B="$OPTARG"
;;
        c) C="$OPTARG"
;;
        :) echo "bla"
exit 1
;;
esac
done

echo "a: $A, b: $B, c: $C"

运行 #1: 期望的结果:

user@host $  ./check.sh -a asd -b "asd|asd -x y" -c asd
a: -a asd -b "asd|asd -x y" -c asd, b: ,c: 

实际结果:

user@host $  ./check.sh -a asd -b "asd|asd -x y" -c asd
a: -a, b: , c:

运行#2: 期望的结果:

user@host $ ./check_params.sh -a asd -b asd|asd -c asd
a: -a asd -b asd|asd -c asd, b: ,c:

实际结果:

user@host $ ./check_params.sh -a asd -b asd|asd -c asd
-bash: asd: command not found

使用$*代替$@:

check.sh:

#!/bin/bash
./b.sh -a "$*"

"$*" 是所有位置参数与 $IFS 变量连接在一起的字符串表示。而 $@ 扩展为单独的参数。

另请注意,在您的第二个示例中,您需要使用引号管道字符串:

./check.sh -a asd -b 'asd|asd' -c asd

Check: What is the difference between “$@” and “$*” in Bash?