bash 通过 SSH 执行命令时不能使用别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22537699/
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
Cannot use alias while executing a command via SSH
提问by davidhq
I commented out this line in .bashrc:
我在 .bashrc 中注释掉了这一行:
# [ -z "$PS1" ] && return
and now the alias gets read, but I still cannot execute it... :/
现在别名被读取,但我仍然无法执行它......:/
We can ask the server if the alias has been defined:
我们可以询问服务器是否已经定义了别名:
$ ssh server "cd /tmp && alias backup_tb"
alias backup_tb='pg_dump -U david tb > tb.sql'
But it is not expanded:
但它没有扩展:
$ ssh server "cd /tmp && backup_tb"
bash: backup_tb: command not found
Any ideas?
有任何想法吗?
回答by stanleyxu2005
Quoted from the man page of bash
: Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt ...
引自手册页bash
:当 shell 不是交互式时,别名不会扩展,除非使用 shopt 设置 expand_aliases shell 选项...
So the simplest way IMO is to put the following lines at the top of your /home/<user>/.bashrc
file:
因此,IMO 最简单的方法是将以下几行放在/home/<user>/.bashrc
文件的顶部:
# comment out the original line
# [ -z "$PS1" ] && return
if [ -z "$PS1" ]; then
shopt -s expand_aliases
# alias ls='ls --color=always'
# return
fi
Save and exit. Now you can run ssh user@host "your_alias"
successfully.
保存并退出。现在你可以ssh user@host "your_alias"
成功运行了。
回答by yekirihi
# WARNING!!!! Just effective in openssh-server.!!!!!!!
# cd your loacl user .ssh path
# if you want to make all user has the same alias you should go to
#'/etc/ssh' touch a file 'sshrc'
# what is 'sshrc'? he has the same function as 'rc.local',just a shell
# script when you first login.
# The following is a configuration of the current user .
cd ~/.ssh
touch sshrc
chmod +x sshrc
#edit sshrc and type
alias ll="ls -lah --color"
#Apply changes
source sshrc