Why doesn't my alias work over ssh?
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 tellsbash
to run an interactive shell. Aliases are enabled by default only in interactive shells.The
-t
options tellsssh
to allocate a pseudo-tty. Without this,bash
emits a warning message when started in interactive mode. This also enablesls
colors. Without it, you'd have to use--color=always
, seeman 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.
Related videos on Youtube
Zanna
Updated on September 18, 2022Comments
-
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 almost 8 years
.bashrc
is only read if the shell is interactive. -
Terrance almost 8 yearsWith 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 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 almost 8 yearsAha 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 almost 8 yearsWhat terminal application are you using? I am doing my tests in xfce4-terminal.
-
Matei David almost 8 yearsI edited the answer to address the color issue.
-
Zanna almost 8 years@Terrance MATE terminal...
-
Terrance almost 8 yearsIs the color still not showing up with the answer Matei gave below?
-
Zanna almost 8 years@Terrance yes it does work (as I commented on the answer)
-
-
Zanna almost 8 yearsthat works.... to your explanation, sure, but I am
ssh
ing to myself :) -
Matei David almost 8 yearsI meant in general when using
ssh
. Yes, withlocalhost
that's not a problem. -
Matei David almost 8 years
ssh ... <cmd>
exits when<cmd>
is done. If you want to keep the shell around afterls
, tryssh localhost -t 'bash -ci "l.; exec bash"'
. -
Zanna almost 8 yearsAha I understand. Thanks a lot. I'm going to delete my comments :)