8

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?

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
davidhq
  • 4,660
  • 6
  • 30
  • 40
  • 1
    Note these are different things. The alias in `.bashrc` apply for the server where this file is stored, while you are trying to use them in a `ssh` command, so in another server. – fedorqui Mar 20 '14 at 15:31
  • @fedorqui He actually defined the alias in the remote `.bashrc` The first ssh command *shows* that the definition exists. The second shows that it isn't honoured. – Henk Langeveld Mar 20 '14 at 15:38
  • @HenkLangeveld you are totally right, didn't pay enough attention. – fedorqui Mar 20 '14 at 15:42
  • I edited this for clarity and split the two ssh sections. – Henk Langeveld Mar 20 '14 at 15:43
  • I am thinking that it might have to do with `.bashrc` or `.bash_profile`. [What's the difference between .bashrc, .bash_profile, and .environment?](http://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment) – fedorqui Mar 20 '14 at 15:53
  • tried to symlink .bash_profile to .bashrc ... this helped in some other case... but not here – davidhq Mar 20 '14 at 16:17

2 Answers2

16

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.

stanleyxu2005
  • 8,081
  • 14
  • 59
  • 94
  • ah yes! I was testing this one before but I haven't commented out the line `"[ -z "$PS1" ] && return"` yet then... So it didn't work because .bash_aliases didn't get loaded. So the solution is to do *both* of these things. Thank you! – davidhq Mar 20 '14 at 17:23
  • 1
    I would rephrase or drop that last sentence about `ssh user@host "alias"`. The question already showed that the `alias` command works with our without the `shopt -s ...`. – Henk Langeveld Mar 20 '14 at 19:08
-1
# 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 
yekirihi
  • 1
  • 2
  • 1
    explain it please – Muhammad Muazzam May 14 '17 at 07:24
  • sshrc like bashrc it will be run when login in – yekirihi May 14 '17 at 16:07
  • Forget to say that only openssh effective. – yekirihi May 14 '17 at 16:22
  • 2
    Explain it **in the answer itself** please. Note that [comments are temporary and could be deleted anytime](https://stackoverflow.com/help/privileges/comment). If you have additional information to provide, please update your answer by clicking on the **"[edit]"** link under the post. [See here](https://meta.stackexchange.com/q/19756/204869) for details. Thank you. – Pang May 15 '17 at 01:18