Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use alias while executing a command via SSH

Tags:

bash

ssh

I commented out this line in .bashrc:

# [ -z "$PS1" ] && return

and now the alias gets read, but I still cannot execute it... :/

We can ask the server if the alias has been defined:

$ ssh server "cd /tmp && alias backup_tb"
alias backup_tb='pg_dump -U david tb > tb.sql'

But it is not expanded:

$ ssh server "cd /tmp && backup_tb"
bash: backup_tb: command not found

Any ideas?

like image 766
davidhq Avatar asked Mar 20 '14 15:03

davidhq


1 Answers

Quoted from the man page of bash: Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt ...

So the simplest way IMO is to put the following lines at the top of your /home/<user>/.bashrc file:

# comment out the original line
# [ -z "$PS1" ] && return

if [ -z "$PS1" ]; then
  shopt -s expand_aliases
  # alias ls='ls --color=always'
  # return
fi

Save and exit. Now you can run ssh user@host "your_alias" successfully.

like image 99
stanleyxu2005 Avatar answered Oct 01 '22 17:10

stanleyxu2005