将所有脚本参数复制到另一个变量 [英] Copy all script arguments to another variable

查看:71
本文介绍了将所有脚本参数复制到另一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要复制所有脚本参数并将它们传递给另一个脚本.我试图这样做:

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"

然后打印

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

我应该如何在bash中执行此操作,或者与POSIX兼容?

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

推荐答案

$@就像一个数组,因此您的临时存储区必须是一个数组:

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

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

然后使用它们:

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

这篇关于将所有脚本参数复制到另一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆