ssh command execution doesn't consider .bashrc | .bash_login | .ssh/rc?

Viewed 43729

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?

5 Answers

EDIT:

As pointed out here about non-interactive shells..


 # If not running interactively, don't do anything
[ -z "$PS1" ] && return
 # execution returns after this line

Now, for every alias in your bashrc file say i have:


alias ll="ls -l"
alias cls="clear;ls" 

Create a file named after that alias say for ll:

user@host$ vi ssh_aliases/ll
#inside ll,write
ls -l
user@host$ chmod a+x ll

Now edit .bashrc to include:


 # If not running interactively, don't do anything
[ -z "$PS1" ] && export $PATH=$PATH:~/ssh_aliases

This does the job.. although I am not sure if it is the best way to do so
EDIT(2)
You only need to do this for aliases, other commands in bashrc will be executed as pointed out by David "you must have executable for ssh to run commands".

Related