Non-interactive shell expand alias

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).


The shell that you get when you execute a command remotely with SSH is neither an interactive shell nor a login shell:

$ ssh server 'bash -c "echo $-"'
chsB

(there's no i and no l in the response)

In Bash's case that means that none of the usual initialization files are read.

You can force the remote shell to be a login shell by adding -l to your Bash invocation, which means that it would parse the first one of ~/.bash_profile, ~/.bash_login, and ~/.profile that it can find, searching in that order, but not ~/.bashrc. This means that you will have to put your aliases in one of those files instead.


I had the same problem, and at first shopt -s expand_aliases didn't seem to help. What I've found out is that this options should be set before adding the actual aliases. So if aliases are created before your .bashrc sets the expand_aliases options, they won't be available. Therefore, you should load (or reload) aliases after setting the option.