小编zom*_*rot的帖子

有什么方法可以禁用 bash 中的分词吗?

我不是 bash 专家,但最近我一直在编写 bash 脚本,而且每次都引用我的参数真的很麻烦。有什么办法可以改变这种 shell 行为吗?我希望我的字符串即使不引用它们也不会被分割

PS - 将 IFS 设置为 null 不起作用,因为所有参数都会被破坏。我想要的是写作时:

func1() {
    echo $1 $2 $3
}

func2() {
    echo "number of arguments func2 got: $#"
} 

func2 $(func1 firstarg "second arg is a multi word string" "third arg is also a multi word string")
Run Code Online (Sandbox Code Playgroud)

会打印:

number of arguments func2 got: 3
Run Code Online (Sandbox Code Playgroud)

编辑,感谢评论指出:由于我的示例使用 echo,因此代码无论如何都会打印它得到 1 或 14 个参数,具体取决于您是否引用 func2 的参数。然而,我正在寻找一种从函数返回多个值的方法,而不会使它们被破坏,就像我提到的预期结果一样

谢谢!

bash

1
推荐指数
1
解决办法
2002
查看次数

标签 统计

bash ×1