Unix“$ @”作为参数


0

说我有一些命令:

somecommand "$@"

$ @有什么意义?它一定是我不熟悉的一些unix技巧。不幸的是,由于它的所有标点符号,我也不能谷歌它。

Answers:


5

它是当前的shell脚本或函数参数,单独引用。

man bash 说:

@ 从1开始扩展到位置参数。当扩展发生在双引号内时,每个参数都会扩展为单独的单词。那是, "$@" 相当于 "$1" "$2" ...


给出以下脚本:

#!/usr/bin/env bash

function all_args {
    # repeat until there are no more arguments
    while [ $# -gt 0 ] ; do
        # print first argument to the function
        echo $1
        # remove first argument, shifting the others 1 position to the left
        shift
    done
}

echo "Quoted:"
all_args "$@"
echo "Unquoted:"
all_args $@

这在执行时发生:

$ ./demo.sh foo bar "baz qux"
Quoted:
foo
bar
baz qux
Unquoted:
foo
bar
baz
qux

你可以通过搜索找到它 \$@man bash。你需要摆脱美元的性格。
Daniel Beck
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.