bash 在bash脚本中转义冒号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41743213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:38:00  来源:igfitidea点击:

escaping colon in bash script

linuxbashshellescaping

提问by PhoenixBlue

Good day all, I am trying to write a script in which a command should contain the colon character ':'. Looking at this document, characters can be escaped using this character '^', but they do not mention the colon character.

大家好,我正在尝试编写一个脚本,其中的命令应包含冒号字符“:”。查看此文档,可以使用此字符 '^' 对字符进行转义,但它们没有提及冒号字符。

The command should be:

命令应该是:

iwconfig wlan0 key s:AsciiPassword

So in my script I have something like:

所以在我的脚本中,我有类似的东西:

iwconfig $interface key s:$password

When I run the first command manually, it obviously does what is expected, but the second one in the script, Nothing!!! Would appreciate any help

当我手动运行第一个命令时,它显然做了预期的事情,但是脚本中的第二个,什么都没有!!!将不胜感激任何帮助

Update: I cannot really produce an output because the command does not produce any.

更新:我无法真正产生输出,因为该命令不产生任何输出。

回答by linedash

That document you link relates to windows.

您链接的那个文档与 Windows 相关。

In bash ; escape with '\'.

在 bash 中;用“\”转义。

You also likely want to quote your variable expansions

您可能还想引用您的变量扩展

I would imagine you need:

我想你需要:

iwconfig "${interface}" key s:"${password}"

You mayneed to escape the :, though I doubt it. If so it would be:

可能需要逃避 :,尽管我对此表示怀疑。如果是这样,那就是:

iwconfig "${interface}" key s\:"${password}"