0

I know I can execute a function through SSH on a remote server like so:

$ ssh remote_server "$(declare -f cool_function); cool_function"

I also have some aliases that I would like to execute on remote servers without having to define them on any file sourced on the remote servers, so I can have all my functions and aliases in one place and use them on any remote SSH server.

I thought this would work:

$ alias very_cool_alias='echo A very cool alias'
$ very_cool_alias
A very cool alias
$ ssh remote_server "$(alias very_cool_alias);very_cool_alias"
bash: very_cool_alias: command not found

Although if I check for aliases the alias is there:

$ ssh remote_server "$(alias very_cool_alias);alias"
alias very_cool_alias='echo A very cool alias'

Why the function works but not the alias, although it is passed to the remote SSH server as the last example proves? Any idea on what I'm missing or how can I achieve this?

I have also tried setting the expand_aliases option but still get command not found:

ssh remote_server "shopt -s expand_aliases;$(alias some_alias);some_alias"    
2

Shell manual tells us:

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

However it also mentions

The rules concerning the definition and use of aliases are somewhat confusing. Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed.

This can be tested locally: (ssh is invoking bash -c remotely):

$ bash -c "shopt -s expand_aliases; shopt expand_aliases;alias x=ls; x"
expand_aliases  on
bash: x: command not found

It does help to make it multi-line:

$ bash -c "shopt -s expand_aliases; alias x='echo y'
> x"
y

And this also works with ssh.

|improve this answer|||||
  • I had already tried doing ssh remote_server "shopt -s expand_aliases;$(alias some_alias);some_alias" but still get command not found... Any ideas? – MauricioRobayo May 14 '17 at 1:26
  • @archimiro I updated my answer after trying it out. To me however it looks like you dont want to use aliases at all. – eckes May 14 '17 at 10:12
  • Thanks for the multiline hint, that got me ! – rag Mar 4 '19 at 14:16
-2

You should probably synchronize a .profile, .bash_profile and/or .bashrc file containing your favourite aliases. I suggest using rsync for convenience.

|improve this answer|||||
  • Thanks, but that is what I'm trying to avoid, as I said: "without having to define them on any file sourced on the remote servers". – MauricioRobayo May 13 '17 at 20:16
  • But you're not saying why, so I'm fishing here. Is the problem that you don't want to perform the sync commands, or is the problem that you don't want to have any files on the remotes? Because I'm thinking that LocalCommand could help you out in the first case. In the second case: what reason do you have for not syncing your preferences? – Cheatah May 13 '17 at 20:27
  • The problem is why is it possible to do that with a function but not with an alias, and how to achieve the same or similar result with an alias if wherever possible? – MauricioRobayo May 14 '17 at 0:45
  • Well something like this works: echo -e "shopt -s expand_aliases; alias x=ls\nx" | ssh remote_server – Cheatah May 14 '17 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.