Bash – How to Expand Alias in Non-Interactive Shell

aliasbashbashrcssh

I'm having trouble getting aliases to expand on my hosting account when I run a command like:

ssh user@server "bash -c \"alias\""

My .bashrc file is:

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"

When I run the above ssh command, I do see ".bashrc" echo'd. But if I try to run aliases, I get nothing.

I could try "bash -ic", but this is actually in a script that I can't easily change, and I want to know why this isn't working.

Output of 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

Output of ssh user@server "bash -c \"echo $SHELL\""

.bashrc
/bin/bash

Best Answer

From the bash(1) man page:

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below).

Related Question