如何在 Bash 参数中保留空格

Fra*_*a ت 7 unix bash arguments command-line-arguments

我使用命令:

cm1 cm2 arg1 arg2 'argument 3'
Run Code Online (Sandbox Code Playgroud)

它首先转到cm1,然后重定向arg1 arg2 'argument 3'到另一个文件。

/usr/bin/cm1

#! /bin/bash
# some script here
shift
cm2 $@
Run Code Online (Sandbox Code Playgroud)

/usr/bin/cm2

echo $#
# This returns 4 in lieu of 3 because the white space in 'argument 3' causes the argument to be split into two arguments.
Run Code Online (Sandbox Code Playgroud)

那么,如何将参数从一个脚本传递到另一个脚本并确保不会将空格作为参数分隔符读取?

Der*_*ler 7

我假设您必须将其重新包装为引号,如下所示:

#! /bin/bash
# some script here
shift
cm2 "$@"
Run Code Online (Sandbox Code Playgroud)