问题描述
我生成了一个包含我所有参数的 bash 变量,这些参数包含空格.当我使用这些参数启动命令时 - 例如.ls $args - 引号没有正确解释.这是一个示例 - 还创建和删除所需的文件.
I generate a bash variable containing all my args and those args contain spaces.When I launch a command with those args - eg. ls $args - quotes are not correctly interpreted.Here is an example - also creating and erasing needed files.
#!/bin/bash
f1="file n1"
f2="file n2"
# create files
touch "$f1" "$f2"
# concatenate arguments
args=""$f1" "$f2""
# Print arguments, then launch 'ls' command
echo "arguments :" $args
ls $args
# delete files
rm "$f1" "$f2"
这样,对于 "file、n1"、"file 和 n2,我有一些没有这样的文件"错误"
With that, I have some "no such file" errors for "file, n1", "file and n2"
推荐答案
您可以考虑使用 array 用于 args,如下所示:
You might consider using an array for the args, something like this:
args=( "$f1" "$f2" )
ls "${args[@]}"
(您目前遇到的问题是,一旦发生插值,文件内和文件间命名空间之间就没有区别.)
(The problem you're hitting at the moment is that once interpolation has happened there's no difference between intra- and inter- filename spaces.)
这篇关于在 Bash 中包含多个带引号的 args 的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!