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

BashSsh

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

Instead of:

ssh user@host "bash -c ll"

try:

ssh user@host "bash -ic ll"

to force bash to use an "interactive shell".

Solution 3 - Bash

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
user@host$ vi ssh_aliases/ll
#inside ll,write
ls -l
user@host$ chmod a+x ll

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

[ -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".

Solution 4 - Bash

an alternative to alias that will be visible in all script is EXPORT & EXECUTE VARIABLE

# shortcut to set enviroment to insensitive case
export go_I="shopt -s nocasematch"

Now in any script you can use

#!/bin/bash $go_I # go Insensitive [[ a == A ]] # evaluates TRUE ( $? == 0) $go_C # maibe want to go back to casesensitive

it's useful to place all shortcuts/aliases in /path/to/my_commands and edit /etc/bash.bashrc

source /path/to/my_commands

Solution 5 - Bash

Open file ~/.bash_profile. If this file does not exist create one in the home directory and add the below line

source = $HOME/.bashrc

exit your ssh and login agian and you should get the .bashrc settings working for you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionblackicecubeView Question on Stackoverflow
Solution 1 - BashJoey MazzarelliView Answer on Stackoverflow
Solution 2 - BashFred StlukaView Answer on Stackoverflow
Solution 3 - Bashsud03rView Answer on Stackoverflow
Solution 4 - BashbortunacView Answer on Stackoverflow
Solution 5 - BashSravan ThokalaView Answer on Stackoverflow