Why doesn't my alias work over ssh?

14,834

Try:

ssh localhost -t bash -ci l.

Note:

  • The alias should be in ~/.bashrc on the remote server, not on your local machine.

  • The -i option tells bash to run an interactive shell. Aliases are enabled by default only in interactive shells.

  • The -t options tells ssh to allocate a pseudo-tty. Without this, bash emits a warning message when started in interactive mode. This also enables ls colors. Without it, you'd have to use --color=always, see man ls.

  • There is another way to enable aliases, without setting the interactive flag, namely shopt -s expand_aliases. So you could try:

    ssh localhost 'bash -c "shopt -s expand_aliases; l."'
    

    However:

    • Your .bashrc might only define aliases if the shell sourcing it is interactive. In this example, the shell would not be interactive at that time.

    • If you try to define aliases on the same line, see this.

Share:
14,834

Related videos on Youtube

Zanna
Author by

Zanna

Updated on September 18, 2022

Comments

  • Zanna
    Zanna almost 2 years

    I have an alias defined in my .bashrc

    alias l.='ls -d .* --color=auto'
    

    It's very useful :) but it doesn't work via ssh:

    $ ssh localhost l.
    bash: l.: command not found
    

    Why is that?

    • user4556274
      user4556274 almost 8 years
      .bashrc is only read if the shell is interactive.
    • Terrance
      Terrance almost 8 years
      With your alias over ssh, there will probably be no color, where if you change your alias to alias l.='ls -d .* --color' then the colors appear. Just thought I would add that. At least I was experiencing that.
    • Zanna
      Zanna almost 8 years
      @Terrance I was wondering about that... I still get no colour (and no columns) although I get colour (and columns) as before after changing the alias (and doing source .bashrc)
    • Zanna
      Zanna almost 8 years
      Aha I get columns! I know C is for columns but normally I get them without asking... still no colour... (I'm not spelling it the British way in my alias, promise ;) ) @Terrance
    • Terrance
      Terrance almost 8 years
      What terminal application are you using? I am doing my tests in xfce4-terminal.
    • Matei David
      Matei David almost 8 years
      I edited the answer to address the color issue.
    • Zanna
      Zanna almost 8 years
      @Terrance MATE terminal...
    • Terrance
      Terrance almost 8 years
      Is the color still not showing up with the answer Matei gave below?
    • Zanna
      Zanna almost 8 years
      @Terrance yes it does work (as I commented on the answer)
  • Zanna
    Zanna almost 8 years
    that works.... to your explanation, sure, but I am ssh ing to myself :)
  • Matei David
    Matei David almost 8 years
    I meant in general when using ssh. Yes, with localhost that's not a problem.
  • Matei David
    Matei David almost 8 years
    ssh ... <cmd> exits when <cmd> is done. If you want to keep the shell around after ls, try ssh localhost -t 'bash -ci "l.; exec bash"'.
  • Zanna
    Zanna almost 8 years
    Aha I understand. Thanks a lot. I'm going to delete my comments :)