Is it possible somehow to source a specific bashfile right at the start of each ssh session on a foreign host? I have a file with just aliases that I want to use on foreign hosts as well as on my local machine. Now I'm fully aware that not all commands might be available on the foreign host, still my aliases usually only wrap very basic things like ls -al | grep $1
, so I think that there's no harm, however the internet tells me that I always need to create a seperate .bashrc
file on every host (which I can't or won't do because the whole company uses the same user on the external machine and I don't want to force my aliases up anyone's throat).
What's the least cumbersome way to achieve this?
Suppose your remote login shell is also Bash —
First, define this in your local Bash shell:
#
# update it per your requirement
#
function rcssh()
{
local rcfile=$1
local dest=$2
local rc=$( < "$rcfile" )
printf -v rc '%q' "$rc"
ssh -t $dest "bash --rcfile <( printf '%s\n' $rc )"
}
Next, save all your aliases (and other bashrc stuff) in a local file, say myrcfile
(suppose it's not too big):
echo 'Loading local rc on remote ...'
alias foo='echo hello foo'
function bar()
{
echo hello bar
}
echo 'Loading local rc on remote ... done'
#
# if you also want to load some remote rcfile
#
source ~/.bashrc
Then, run like this:
[local] $ rcssh myrcfile user@server
Loading local rc on remote ...
Loading local rc on remote ... done
bash-3.2$ foo
hello foo
bash-3.2$ bar
hello bar
bash-3.2$