小编Lab*_*dor的帖子

从脚本中传递包含空格的字符串作为命令行参数

我在 mac 上使用 bash shell。

我想编写一个 shell 脚本 'gac' 以便运行

> gac one two three
Run Code Online (Sandbox Code Playgroud)

产生与跑步完全相同的效果

> git add .
> git commit -m "one two three"
Run Code Online (Sandbox Code Playgroud)

到目前为止,我的脚本是这样的:

function gac() {
    git add .
    concatenated="'$*'"
    git commit -m "$concatenated"
}
Run Code Online (Sandbox Code Playgroud)

我发现伎俩拼接外壳参数为单个空格分隔字符串(脚本的第二行)在这里。上面的脚本几乎可以工作,除了日志消息读取

a382806 'one two three'
Run Code Online (Sandbox Code Playgroud)

当我使用我的 shell 脚本时,而不是

a382806 one two three
Run Code Online (Sandbox Code Playgroud)

就像我手动输入时一样

> git commit -m "one two three"
Run Code Online (Sandbox Code Playgroud)

在命令行上。

有任何想法吗?

shell scripting bash quoting

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

标签 统计

bash ×1

quoting ×1

scripting ×1

shell ×1