非交互式 shell 扩展别名

Mat*_*att 12 bash ssh alias bashrc

当我运行如下命令时,我无法获取别名以扩展我的主机帐户:

ssh user@server "bash -c \"alias\""
Run Code Online (Sandbox Code Playgroud)

我的 .bashrc 文件是:

echo .bashrc
# .bashrc

shopt -s expand_aliases

# Source global definitions (commenting this out does nothing)
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
alias php="php55"
alias composer="php ~/bin/composer.phar"
Run Code Online (Sandbox Code Playgroud)

当我运行上面的 ssh 命令时,我确实看到了“.bashrc”回显。但是如果我尝试运行别名,我什么也得不到。

我可以尝试“bash -ic”,但这实际上是在我无法轻易更改的脚本中,我想知道为什么这不起作用。

输出 ssh user@server "bash -c \"shopt\""

.bashrc
autocd          off
cdable_vars     off
cdspell         off
checkhash       off
checkjobs       off
checkwinsize    off
cmdhist         on
compat31        off
compat32        off
compat40        off
dirspell        off
dotglob         off
execfail        off
expand_aliases  off
extdebug        off
extglob         off
extquote        on
failglob        off
force_fignore   on
globstar        off
gnu_errfmt      off
histappend      off
histreedit      off
histverify      off
hostcomplete    on
huponexit       off
interactive_comments    on
lithist         off
login_shell     off
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off
Run Code Online (Sandbox Code Playgroud)

输出 ssh user@server "bash -c \"echo $SHELL\""

.bashrc
/bin/bash
Run Code Online (Sandbox Code Playgroud)

小智 16

bash(1)手册页:

当 shell 不是交互式时,别名不会扩展,除非使用 shopt 设置 expand_aliases shell 选项(请参阅下面的 SHELL BUILTIN COMMANDS 下的 shopt 描述)。

  • 我的 .bashrc 中确实有 `shopt -s expand_aliases`,但这似乎不起作用。我不知道为什么不,但我想这通常是答案 (5认同)

Kus*_*nda 8

使用 SSH 远程执行命令时获得的 shell 既不是交互式 shell,也不是登录 shell:

$ ssh server 'bash -c "echo $-"'
chsB
Run Code Online (Sandbox Code Playgroud)

(回复中没有i也没有l

在 Bash 的情况下,这意味着不会读取任何通常的初始化文件。

您可以强制远程shell加入是一个登录shell-l到你的Bash调用,这意味着它会分析的第一个~/.bash_profile~/.bash_login~/.profile它可以找到,按顺序搜索,但没有~/.bashrc。这意味着您必须将别名放在这些文件之一中。