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

# 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\""

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\""

  • I've given up and just created symlinks and scripts in ~/bin that do the same thing I wanted with aliases. As long as I export my $PATH as ~/bin:$PATH it works well.
    – Matt
    Sep 19, 2014 at 17:20
  • On Ubuntu (16.04 and newer for sure) if you have a ~/.local/bin when the /etc/profile is processed it will automatically add that to your PATH. You can also just . /etc/profile after creating the folder to get it added to your PATH without a reboot or logout/login.
    – dragon788
    Sep 20, 2018 at 13:17

5 Answers 5


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

  • 6
    I do have shopt -s expand_aliases in my .bashrc, but that doesn't seem to work. I'm not sure why not, but I guess this would be the answer normally
    – Matt
    Sep 19, 2014 at 17:19
  • @Matt I'm not sure the order of 'events', but did you try making sure the shopt -s expand_aliases is in your .bashrc before the aliases? Certainly, your output in the question shows expand_aliases is off. Sep 23, 2014 at 14:44
  • Yeah, see the .bashrc in my question. I see the echo line but shopt does show it as off. Maybe there's just some weird server setting that's preventing this, I don't know. I've got a workaround
    – Matt
    Sep 24, 2014 at 7:28
  • probably the .bashrc file was not being sourced by default.
    – erik258
    Dec 29, 2017 at 23:15

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 $-"'

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

  • This sounds like the solution. I'm not even using this server any more so I can't test it, but probably worth upvoting this in case someone else finds the question.
    – Matt
    Jun 19, 2016 at 17:51

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.


bash(1) says


When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.


An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option.

so, obviously, you either source .bashrc manually or run it with -i

if aliases are all you need i would recommend splitting them away in e.g. .aliases and then source that from both .bashrc and your script, just in case stuff creeps up in .bashrc that may break your script, as it often happens

  • so I see that .bashrc is being sourced. I see the echo line from .bashrc when I run the command. The problem is aliases in that file don't expand.
    – Matt
    Sep 18, 2014 at 15:46

You can solve any problem by entering:

if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi

at the first line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .