How to access command line arguments of the caller inside a function?

我试图用 bash 编写一个函数,它将访问脚本命令行参数,但是它们被函数的位置参数替换。如果命令行参数没有显式传入,那么函数是否有办法访问它们?

# Demo function
function stuff {
echo $0 $*
}


# Echo's the name of the script, but no command line arguments
stuff


# Echo's everything I want, but trying to avoid
stuff $*
179121 次浏览
#!/usr/bin/env bash


echo name of script is $0
echo first argument is $1
echo second argument is $2
echo seventeenth argument is $17
echo number of arguments is $#

编辑: 请看我对问题的评论

如果你想要你的参数 C 样式(参数数组 + 参数数量) ,你可以使用 $@$#

$#给出参数的个数。
$@给你所有的参数。你可以通过 args=("$@")把它变成一个数组。

例如:

args=("$@")
echo $# arguments passed
echo ${args[0]} ${args[1]} ${args[2]}

Note that here ${args[0]} actually is the 1st argument and not the name of your script.

# Save the script arguments
SCRIPT_NAME=$0
ARG_1=$1
ARGS_ALL=$*


function stuff {
# use script args via the variables you saved
# or the function args via $
echo $0 $*
}




# Call the function with arguments
stuff 1 2 3 4

拉维的评论本质上就是答案。函数有自己的参数。如果希望它们与命令行参数相同,则必须将它们传入。否则,您显然是在调用一个没有参数的函数。

也就是说,如果您希望将命令行参数存储在一个全局数组中,以便在其他函数中使用,那么可以这样做:

my_function() {
echo "stored arguments:"
for arg in "${commandline_args[@]}"; do
echo "    $arg"
done
}


commandline_args=("$@")


my_function

您必须通过 commandline_args变量访问命令行参数,而不是 $@$1$2等等,但它们是可用的。我不知道任何直接赋值参数数组的方法,但是如果有人知道,请告诉我!

另外,请注意我使用和引用 $@的方式——这是确保特殊字符(空格)不会被弄乱的方式。

My reading of the Bash 参考手册 says this stuff is captured in BASH _ ARGV, 尽管它经常提到“堆栈”。

#!/bin/bash


shopt -s extdebug


function argv {
for a in ${BASH_ARGV[*]} ; do
echo -n "$a "
done
echo
}


function f {
echo f $1 $2 $3
echo -n f ; argv
}


function g {
echo g $1 $2 $3
echo -n g; argv
f
}


f boo bar baz
g goo gar gaz

f.sh中保存

$ ./f.sh arg0 arg1 arg2
f boo bar baz
fbaz bar boo arg2 arg1 arg0
g goo gar gaz
ggaz gar goo arg2 arg1 arg0
f
fgaz gar goo arg2 arg1 arg0

您可以使用 shift 关键字(操作符?)来遍历它们。 例如:

#!/bin/bash
function print()
{
while [ $# -gt 0 ]
do
echo "$1"
shift 1
done
}
print "$@"

一个人也可以这样做

#!/bin/bash
# script_name function_test.sh
function argument(){
for i in $@;do
echo $i
done;
}
argument $@

现在把你的剧本叫做

./function_test.sh argument1 argument2

我是这样做的:

#! /bin/bash


ORIGARGS="$@"


function init(){
ORIGOPT= "- $ORIGARGS -" # tacs are for sed -E
echo "$ORIGOPT"
}

将参数从命令行传递到特定函数的最简单、也可能是最好的方法是将参数直接包含在函数调用中。

# first you define your function
function func_ImportantPrints() {
printf '%s\n' "$1"
printf '%s\n' "$2"
printf '%s\n' "$3"
}
# then when you make your function call you do this:
func_ImportantPrints "$@"

This is useful no matter if you are sending the arguments to main or some function like func_parseArguments (a function containing a case statement as seen in previous examples) or any function in the script.

This is @mcarifio response with several comments incorporated:

#!/bin/bash


shopt -s extdebug


function stuff() {
local argIndex="${#BASH_ARGV[@]}"
while [[ argIndex -gt 0 ]] ; do
argIndex=$((argIndex - 1))
echo -n "${BASH_ARGV[$argIndex]} "
done
echo
}


stuff

我想强调的是: