Copy all script arguments to another variable

跟風遠走 提交于 2019-12-01 07:28:05

问题


I need to copy all script arguments and pass them to another script. I have tried to do it like this:

args=$@    
printargs.sh $args
echo ------
printargs.sh "$args"

but in such case if i call my parent script with arguments containing spaces e.g.:

script.sh "arg 1" "arg 2"

then it prints

arg
1
arg
2
----
arg 1 arg 2

How should I do this in bash or alternatively to be compatible with POSIX?


回答1:


$@ is like an array, so your temporary storage needs to be an array:

args=( "$@" )      # quotes are needed there

And then to use them:

printargs.sh "${args[@]}"


来源:https://stackoverflow.com/questions/29536328/copy-all-script-arguments-to-another-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!