处理Bash命令参数中的引号和转义空格 [英] Handling quoting and escaped spaces in Bash command arguments

查看:168
本文介绍了处理Bash命令参数中的引号和转义空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个bash脚本,该脚本使用其他输入来为命令构建一组复杂的参数.到目前为止,该脚本可以使用此技术完美运行:

I am writing a bash script that uses other inputs to build a set of complicated arguments for a command. So far, the script runs perfectly with this technique:

whatIwant="command \"$filename-with-spaces-maybe\" -opt1 \"$some_words\" -opt2 $a_number  -opt3 \"$a_file_reference\" -opt4 \"$several_sentences\" "
eval $whatIwant

尽管有效,但我对使用eval持谨慎态度,因为文件名错误会造成损坏.我希望能够跳过分配变量然后进行评估的过程,而只是将命令放在行的开头,并由我先前在脚本中构建的适当变量来设置所有参数.

Although it works, I am wary of using eval because of the risk of a bad filename doing damage. I would like to be able to skip assigning the variable and then eval'ing it, in favor of just putting the command at the beginning of the line and having all the arguments be set by the appropriate variables I have built previously in the script.

但是,当我尝试这样做时,文件名似乎没有被视为单个变量,而是作为多个参数传递给命令.我尝试了几种形式的双重转义,但没有设法找到经过正确评估的东西.令人沮丧的是,当我回显生成命令的变量时,它将变量完美复制并粘贴到命令行中并完成我想要的操作.

When I try this, however, the filenames don't seem to be treated as single variables, and instead are passed to the command as multiple arguments. I have tried several forms of double escaping but haven't managed to find something that is correctly evaluated. It's frustrating because when I echo back my variable into which I build the command, it copies and pastes perfectly to the command line and does what I want.

在bash脚本中执行此操作的最佳实践是什么?

What is the best practice for doing this in a bash script?

推荐答案

真的很简单:

your_command "$filename_with_spaces_maybe" -opt1 "$some_words" -opt2 "$a_number" -opt3 "$a_file_reference" -opt4 "$several_sentences"

也就是说:

  • 每个参数扩展(变量名替换为其值)用双引号引起来-即使您认为该值只会是数字.
  • 请勿打扰任何eval或作业等.
  • Put every parameter expansion (where a variable name is replaced with its value) in double quotes -- even if you think the value will only be numeric.
  • Don't bother with any kind of eval, or assignment, etc.

所需的唯一引号是语法而不是文字-嵌套"或转义"引号是不具有语法含义的文字字符,因此它们对解析没有影响(无需执行由eval生成的附加解析步骤,当完全可以避免时,这确实是一个坏习惯),而只是在实际运行的命令中添加多余的不必要的内容.

The only quotes required are syntactic, not literal -- "nested" or "escaped" quotes are literal characters without syntactic meaning, so they have no effect on parsing (without an additional parse step, as generated by eval, which is indeed bad practice when at all avoidable), and just add extra unwanted content to the command actually being run.

如果您认为出于其他原因需要将命令存储在变量中(仅在有条件的情况下添加参数或能够重用它),请参见

For cases when you think you need to store a command in a variable for other reasons (to add arguments only conditionally, or to be able to reuse it), see BashFAQ #50.

如果您尚未确信eval是一个坏主意,则需要阅读 Bash常见问题#48 .

If you weren't already convinced that eval is a Bad Idea, you'd want to read BashFAQ #48.

行情上的Wooledge页面通常是一个很好的资源.

The Wooledge page on Quotes is an excellent resource for this question in general.

这篇关于处理Bash命令参数中的引号和转义空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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