bash 在命令中使用引号和双引号设置别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14123522/
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 04:07:58  来源:igfitidea点击:

Set alias with quotes and double quotes in command

linuxbashshellalias

提问by voy

I want to set an alias for the command:

我想为命令设置别名:

expect -c 'spawn ssh usr@ip -p 57022 ; expect password ; send "pass\n" ; interact'

but quotes from alias alias_name=""and from command doesn't like each other.

但引号 fromalias alias_name=""和 from command 不喜欢对方。

I tried combinations with ', "and `, but all of these failed. How can I do that?

我尝试了与',"和 ` 的组合,但所有这些都失败了。我怎样才能做到这一点?

回答by Rubens

A simple solution is to create a function, instead of an alias:

一个简单的解决方案是创建一个函数,而不是一个别名:

function function_name() {

    expect -c 'spawn ssh usr@ip -p 57022 ; \
        expect password ; send "pass\n" ; interact'

}

So you can call function_name, and it will work just as fine as with an alias.

所以你可以调用function_name,它会像使用别名一样工作。

If you still want to use an alias, just escape the inner "'s:

如果您仍然想使用别名,只需转义内部"的:

alias alias_name="expect -c 'spawn ssh usr@ip -p 57022 ; expect password ; send \"pass\n\" ; interact'"

and it should work.

它应该工作。