Cannot use an alias when executing a command through SSH

I commented on this line in .bashrc:

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

and now the alias is being read, but I still can’t execute it ...: /

We can set the server if an alias was specified:

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

But it does not expand:

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

Any ideas?

+7
bash ssh
source share
2 answers

Quote from the bash man page: Aliases are not expanded if the shell is not interactive, if the shell parameter expand_aliases is not set using shopt ...

So, the easiest way is for the IMO to put the following lines at the beginning 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 successfully run ssh user@host "your_alias" .

+12
source share
 # WARNING!!!! Just effective in openssh-server.!!!!!!! # cd your loacl user .ssh path # if you want to make all user has the same alias you should go to #'/etc/ssh' touch a file 'sshrc' # what is 'sshrc'? he has the same function as 'rc.local',just a shell # script when you first login. # The following is a configuration of the current user . cd ~/.ssh touch sshrc chmod +x sshrc #edit sshrc and type alias ll="ls -lah --color" #Apply changes source sshrc 
0
source share

All Articles