bash ssh 命令执行不考虑 .bashrc | .bash_login | .ssh/rc?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1198378/
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
ssh command execution doesn't consider .bashrc | .bash_login | .ssh/rc?
提问by blackicecube
I am trying to execute a command remotely over ssh, example:
我正在尝试通过 ssh 远程执行命令,例如:
ssh <user>@<host> <command>
The command which needs to be executed is an alias, which is defined in .bashrc, e.g.
需要执行的命令是一个别名,定义在.bashrc中,例如
alias ll='ls -al'
So what in the end the following command should get executed:
那么最后应该执行以下命令:
ssh user@host "ll"
I already found out that .bashrc only gets sourced with interactive shell, so in .bash_login I put:
我已经发现 .bashrc 只能通过交互式 shell 获取源代码,所以在 .bash_login 中我输入了:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
and I also tried to define the alias directly in .bash_login.
我还尝试直接在 .bash_login 中定义别名。
I also tried to put the alias definition / sourcing of .bashrc in .bash_profile and also in .ssh/rc. But nothing of this works. Note that I am not able to change how the ssh command is invoked since this is a part of some binary installation script. The only thing I can modify is the environment. Is there any other possibility to get this alias sourced when the ssh command is executed? Is there some ssh configuration which has to be adapted?
我还尝试将 .bashrc 的别名定义/来源放在 .bash_profile 和 .ssh/rc 中。但这一切都不起作用。请注意,我无法更改 ssh 命令的调用方式,因为这是某些二进制安装脚本的一部分。我唯一可以修改的是环境。执行 ssh 命令时是否还有其他可能来获取此别名?是否有一些必须调整的 ssh 配置?
回答by Joey Mazzarelli
From the man pages of bash:
从 bash 的手册页:
Aliases are not expanded when the shell is not interactive, unless the
expand_aliases
shell option is set usingshopt
当 shell 不是交互式时,别名不会被扩展,除非
expand_aliases
shell 选项是使用设置的shopt
There are a couple ways to do this, but the simplest is to just add the following line to your .bashrc
file:
有几种方法可以做到这一点,但最简单的是将以下行添加到您的.bashrc
文件中:
shopt -s expand_aliases
回答by Fred Stluka
Instead of:
代替:
ssh user@host "bash -c ll"
try:
尝试:
ssh user@host "bash -ic ll"
to force bash to use an "interactive shell".
强制 bash 使用“交互式外壳”。
回答by sud03r
EDIT:
编辑:
As pointed out hereabout non-interactive shells..
正如这里所指出的关于非交互式 shell ..
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# execution returns after this line
Now, for every alias in your bashrc file say i have:
现在,对于 bashrc 文件中的每个别名,请说我有:
alias ll="ls -l"
alias cls="clear;ls"
Create a file named after that alias say for ll:
创建一个以该别名命名的文件,例如 for ll:
user@host$ vi ssh_aliases/ll
#inside ll,write
ls -l
user@host$ chmod a+x ll
Now edit .bashrc to include:
现在编辑 .bashrc 以包括:
# If not running interactively, don't do anything
[ -z "$PS1" ] && export $PATH=$PATH:~/ssh_aliases
This does the job.. although I am not sure if it is the best way to do so
EDIT(2)
You only need to do this for aliases, other commands in bashrc will be executed as pointed out by David "you must have executable for ssh to run commands".
这可以完成工作..虽然我不确定这是否是最好的方法
EDIT(2)
您只需要为别名执行此操作,bashrc 中的其他命令将按照 David 所指出的那样执行“您必须具有可执行文件让 ssh 运行命令”。
回答by bortunac
an alternative to alias that will be visible in all script is EXPORT& EXECUTE VARIABLE
在所有脚本中都可见的别名的替代方法是 EXPORT& EXECUTE VARIABLE
# shortcut to set enviroment to insensitive case
export go_I="shopt -s nocasematch"
Now in any script you can use
现在在您可以使用的任何脚本中
#!/bin/bash
$go_I # go Insensitive
[[ a == A ]] # evaluates TRUE ( $? == 0)
$go_C # maibe want to go back to casesensitive
#!/bin/bash
$go_I # go Insensitive
[[ a == A ]] # evaluates TRUE ( $? == 0)
$go_C # maibe want to go back to casesensitive
it's useful to place all shortcuts/aliases in /path/to/my_commandsand edit /etc/bash.bashrc
将所有快捷方式/别名放在/path/to/my_commands 中并编辑/etc/bash.bashrc很有用
source /path/to/my_commands
source /path/to/my_commands
回答by Sravan Thokala
Open file ~/.bash_profile. If this file does not exist create one in the home directory and add the below line
打开文件 ~/.bash_profile。如果此文件不存在,则在主目录中创建一个并添加以下行
source = $HOME/.bashrc
源 = $HOME/.bashrc
exit your ssh and login agian and you should get the .bashrc settings working for you.
退出你的 ssh 并登录 agian,你应该让 .bashrc 设置为你工作。