tags:

views:

1600

answers:

3

Hi, I am trying to execute a command remotely over ssh, example:

ssh <user>@<host> <command>

The command which needs to be executed is an alias, which is defined in .bashrc, e.g.

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:

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

and I also tried to define the alias directly in .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?

Thanks a lot for any helpful hints!

+2  A: 
Neeraj
Thanks for your suggestion. The aliases invoked by the installation script are defined in a custom bashrc lying somewhere in the installation directory, In that custom bashrc besides aliases there are also lots of export of variables defined. So with your solution I would need to create a file for each alias/export?
blackicecube
hope it works for you now..
Neeraj
I just tried it, unfortunately it still doesn't work. I am not sure but it seems like .bashrc is not sourced when invoking ssh.
blackicecube
try some echo statement before "# If not running interactively, don't do anything" line and invoke first form commandline like:ssh user@machine <cmd> to test whether it works or not. It works on my machine although
Neeraj
+1  A: 

You could tell the shell to execute ll like this:

ssh user@host "bash -c ll"
Joachim Sauer
note that he cannot change how ssh is invoked as it is part of some binary executable..
Neeraj
Oops, didn't see that part, SOrry.
Joachim Sauer
That doesn't work anyways, at least when I tried.
bradlis7
+2  A: 

updated

From the man pages of bash:

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt

There are a couple ways to do this, but the simplest is to just add the following line to your .bashrc file:

shopt -s expand_aliases
Joey Mazzarelli
I upvoted you, but you should explain what it does.
bradlis7
As explained elsewhere here, the shell is not in interactive mode. This enables the option of expanding the aliases.From the docs:If set, aliases are expanded as described above under ALIASES. This option is enabled by default for interactive shells.
Joey Mazzarelli
Yeah, I found it, I was just suggesting that you add it to your comment. I tried doing that command in `ssh $HOST 'shopt -s expand_aliases; ll'`, but that didn't work. It probably works if put it in .bashrc, but I didn't try it.
bradlis7
Oh, ok. If you don't want to change your environment on the destination machine, there are a couple other things you can try: `ssh user@host "bash -ic ll"` works for me by running the command "interactively", and the man pages also talk about setting shopt options with a `-O` flag to bash.
Joey Mazzarelli