以引用形式打印命令行参数
Printing command line arguments in quoted form
$@ 当双引号 ("$@") 扩展为 "$1" "$2" "$3" ...
似乎没有简单的方法来打印 "$@" 中的引号形式
$ set -- "one two" three
$ echo "$@"
one two three
我要找的是
的输出
'one two' three
使用'set -x',是否在“$@”中回显引用形式的参数
$ set -x; echo "$@"; set +x
+ set -x
+ echo 'one two' three
one two three
+ set +x
用户定义的变量 $DOLLAR_AT 里面包含相同的东西,比如
“$@”包含
$ DOLLAR_AT="'one two' three"
转储 $DOLLAR_AT 中的内容,使用 'echo' 按预期工作
$ echo $DOLLAR_AT
'one two' three
由于 shell 对包含特殊字符的参数的某种自动引用
$ set -x; echo $DOLLAR_AT; set +x
+ echo ''\''one' 'two'\''' three
+ set +x
不清楚原因
$ echo "$@"
不会产生与
相同的输出
$ echo "$DOLLAR_AT"
当使用接收到的“$@”从 shell 脚本启动命令时,有一个
在调用之前需要以引用的形式打印出“$@”中的内容
命令。
打印出从
一个 bash 数组。
$ CMDARGS=("one two" "three")
计算双引号内的数组不会以引号形式打印数组元素
$ echo "${CMDARGS[@]}"
one two three
我还需要一种紧凑的方式来打印 $CMDARGS[@] 中的内容
形式类似于这次迭代的结果
$ for ARG in "${CMDARGS[@]}"; do echo \'$ARG\'; done
'one two'
'three'
1。使用参数展开:
您可以尝试内联:
$ set -- 'foo bar' baz
$ echo ${@@Q}
'Foo bar' 'baz'
$ echo ${@@A}
set -- 'Foo bar' 'baz'
$ DOLLAR_AT="'one two' three"
$ echo ${DOLLAR_AT@Q}
''\''one two'\'' three'
$ echo ${DOLLAR_AT@A}
DOLLAR_AT=''\''one two'\'' three'
bash 联机帮助页中的 参数扩展 小节中有更多信息。
${parameter@operator}
Parameter transformation. The expansion is either a transforma‐
tion of the value of parameter or information about parameter
itself, depending on the value of operator. Each operator is a
single letter:
...
Q The expansion is a string that is the value of parameter
quoted in a format that can be reused as input.
...
A The expansion is a string in the form of an assignment
statement or declare command that, if evaluated, will
recreate parameter with its attributes and value.
重复使用:
$ myString1=${@@Q}
$ set -- something\ else
$ . <(echo "set -- $myString1")
$ echo ${@@A}
set -- 'Foo bar' 'baz'
1.1。 $*
和 $@
之间的区别。
简单地$*
用于生成字符串,其中$@
可以生成数组或stringS,视上下文而定。
来自 bash 的联机帮助页:
* Expands to the positional parameters, starting from one...
... That is, "$*" is equivalent to "cc...", where c is
the first character of the value of the IFS variable...
@ Expands to the positional parameters, starting from one...
... That is, "$@" is equivalent to "" "" ...
小演示:
$ paramExpDemo() {
local IFS=+ _arry
_arry="${@@Q}" # var="..." -> context string
declare -p _arry
_arry="${*@Q}"
declare -p _arry
_arry=("${@@Q}") # var=(...) -> context array
declare -p _arry
_arry=("${*@Q}")
declare -p _arry
}
然后
$ paramExpDemo 'foo bar' baz
declare -- _arry="'foo bar'+'baz'"
declare -- _arry="'foo bar'+'baz'"
declare -a _arry=([0]="'foo bar'" [1]="'baz'")
declare -a _arry=([0]="'foo bar'+'baz'")
在上下文 string
、$@
和 $*
中将给出相同的结果,但在上下文 array
, $*
将 return 一个 字符串 当 $@
return 一个数组.
$ set -- 'foo bar' baz
$ Array=("${@@Q}")
$ declare -p Array
declare -a Array=([0]="'foo bar'" [1]="'baz'")
$ echo "${Array[@]@Q}"
''\''foo bar'\''' ''\''baz'\'''
2。使用 printf %q
作为 ,您可以使用:
$ printf '%q\n' "$@"
Foo\ bar
baz
$ printf -v myString2 "%q " "$@"
$ echo "${myString2% }"
Foo\ bar baz
重复使用:
$ set -- something\ else
$ . <(echo "set -- $myString2")
$ echo ${@@A}
set -- 'Foo bar' 'baz'
注意:与一样,这并不完美!事实上,没有办法检索在命令行中使用了哪种语法。
$ printf "<%q>\n" "$myString1" "${myString2% }"
<\'Foo\ bar\'\ \'baz\'>
<Foo\\ bar\ baz>
由于两个字符串都适用于 sourcing 变量,它们并不相同,因此进一步的字符串操作可能会变得复杂...
$@ 当双引号 ("$@") 扩展为 "$1" "$2" "$3" ...
似乎没有简单的方法来打印 "$@" 中的引号形式
$ set -- "one two" three
$ echo "$@"
one two three
我要找的是
的输出'one two' three
使用'set -x',是否在“$@”中回显引用形式的参数
$ set -x; echo "$@"; set +x
+ set -x
+ echo 'one two' three
one two three
+ set +x
用户定义的变量 $DOLLAR_AT 里面包含相同的东西,比如 “$@”包含
$ DOLLAR_AT="'one two' three"
转储 $DOLLAR_AT 中的内容,使用 'echo' 按预期工作
$ echo $DOLLAR_AT
'one two' three
由于 shell 对包含特殊字符的参数的某种自动引用
$ set -x; echo $DOLLAR_AT; set +x
+ echo ''\''one' 'two'\''' three
+ set +x
不清楚原因
$ echo "$@"
不会产生与
相同的输出$ echo "$DOLLAR_AT"
当使用接收到的“$@”从 shell 脚本启动命令时,有一个 在调用之前需要以引用的形式打印出“$@”中的内容 命令。
打印出从 一个 bash 数组。
$ CMDARGS=("one two" "three")
计算双引号内的数组不会以引号形式打印数组元素
$ echo "${CMDARGS[@]}"
one two three
我还需要一种紧凑的方式来打印 $CMDARGS[@] 中的内容 形式类似于这次迭代的结果
$ for ARG in "${CMDARGS[@]}"; do echo \'$ARG\'; done
'one two'
'three'
1。使用参数展开:
您可以尝试内联:
$ set -- 'foo bar' baz
$ echo ${@@Q}
'Foo bar' 'baz'
$ echo ${@@A}
set -- 'Foo bar' 'baz'
$ DOLLAR_AT="'one two' three"
$ echo ${DOLLAR_AT@Q}
''\''one two'\'' three'
$ echo ${DOLLAR_AT@A}
DOLLAR_AT=''\''one two'\'' three'
bash 联机帮助页中的 参数扩展 小节中有更多信息。
${parameter@operator} Parameter transformation. The expansion is either a transforma‐ tion of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter: ... Q The expansion is a string that is the value of parameter quoted in a format that can be reused as input. ... A The expansion is a string in the form of an assignment statement or declare command that, if evaluated, will recreate parameter with its attributes and value.
重复使用:
$ myString1=${@@Q}
$ set -- something\ else
$ . <(echo "set -- $myString1")
$ echo ${@@A}
set -- 'Foo bar' 'baz'
1.1。 $*
和 $@
之间的区别。
简单地$*
用于生成字符串,其中$@
可以生成数组或stringS,视上下文而定。
来自 bash 的联机帮助页:
* Expands to the positional parameters, starting from one... ... That is, "$*" is equivalent to "cc...", where c is the first character of the value of the IFS variable...
@ Expands to the positional parameters, starting from one... ... That is, "$@" is equivalent to "" "" ...
小演示:
$ paramExpDemo() {
local IFS=+ _arry
_arry="${@@Q}" # var="..." -> context string
declare -p _arry
_arry="${*@Q}"
declare -p _arry
_arry=("${@@Q}") # var=(...) -> context array
declare -p _arry
_arry=("${*@Q}")
declare -p _arry
}
然后
$ paramExpDemo 'foo bar' baz
declare -- _arry="'foo bar'+'baz'"
declare -- _arry="'foo bar'+'baz'"
declare -a _arry=([0]="'foo bar'" [1]="'baz'")
declare -a _arry=([0]="'foo bar'+'baz'")
在上下文 string
、$@
和 $*
中将给出相同的结果,但在上下文 array
, $*
将 return 一个 字符串 当 $@
return 一个数组.
$ set -- 'foo bar' baz
$ Array=("${@@Q}")
$ declare -p Array
declare -a Array=([0]="'foo bar'" [1]="'baz'")
$ echo "${Array[@]@Q}"
''\''foo bar'\''' ''\''baz'\'''
2。使用 printf %q
作为
$ printf '%q\n' "$@"
Foo\ bar
baz
$ printf -v myString2 "%q " "$@"
$ echo "${myString2% }"
Foo\ bar baz
重复使用:
$ set -- something\ else
$ . <(echo "set -- $myString2")
$ echo ${@@A}
set -- 'Foo bar' 'baz'
注意:与
$ printf "<%q>\n" "$myString1" "${myString2% }"
<\'Foo\ bar\'\ \'baz\'>
<Foo\\ bar\ baz>
由于两个字符串都适用于 sourcing 变量,它们并不相同,因此进一步的字符串操作可能会变得复杂...