简体   繁体   English

Shell脚本有效,别名失败。 为什么?

[英]shell script works, alias fails. why?

I use a custom gnome-terminal profile for ssh sessions (different bg color provides a nice visual cue that I'm not working locally). 我为ssh会话使用了自定义的gnome-terminal配置文件(不同的bg颜色提供了我不在本地工作的漂亮视觉提示)。 Why doesn't my alias work? 为什么我的别名不起作用?

This almost works, but the terminal window closes immediately. 这几乎可以工作,但是终端窗口会立即关闭。

alias myssh='gnome-terminal --window-with-profile=SSH -e "ssh $1"'

I tried using eval and mixing up the quotes, no change 我尝试使用eval并混合了引号,没有变化

alias myssh='eval gnome-terminal --window-with-profile=SSH -e "/usr/bin/ssh $1"'
alias myssh='gnome-terminal --window-with-profile=SSH -e \"/usr/bin/ssh $1\"'
alias myssh='eval gnome-terminal --window-with-profile=SSH -e \"/usr/bin/ssh $1\"'
alias myssh='gnome-terminal --window-with-profile=SSH -e \"/usr/bin/ssh $1\"'

Aliasing to a script in a file works great. 别名为文件中的脚本效果很好。 What's the difference? 有什么不同?

alias myssh='$HOME/bin/myssh.sh'

file:$HOME/bin/myssh.sh
#!/bin/bash
gnome-terminal --window-with-profile=SSH -e "ssh $1"

In a script you have your arguments in $1 et al. 在脚本中,您可以在$ 1等中使用参数。

An alias is pretty much just text replacement, ie myssh foo will just turn in 别名几乎只是文本替换,即myssh foo会上交

gnome-terminal --window-with-profile=SSH -e "ssh $1" foo

which is obviously wrong. 这显然是错误的。

Just add your script to something like ~/bin. 只需将您的脚本添加到〜/ bin之类的文件中即可。

bash aliases do not take arguments. bash别名不带参数。 You can define a shell function instead: 您可以改为定义Shell函数:

myssh () {
    gnome-terminal --window-with-profile=SSH -e "ssh $1"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM