I have some aliases setup (in this order) in .bashrc:
alias ls="lsc"
alias lsc='ls -Flatr --color=always'
alias lscR='ls -FlatrR --color=always'
Confirming them with alias
after sourcing:
alias ls='lsc'
alias lsc='ls -Flatr --color=always'
alias lscR='ls -FlatrR --color=always'
I can run the newly aliased ls
just fine, and it chains through to the lsc alias, and then executes the command associated with the lsc alias. I can also run lscR
and it operates as expected.
If I try to run lsc
itself though, I get:
$ lsc
lsc: command not found
Any idea why the shell seems to be shadowing/hiding the lsc alias in this scenario? (I realise it's pointless to run 'lsc' when I can just run 'ls' to get the same result here, but I'm trying to understand the shells behaviour in this scenario).
EDIT: Workarounds below for the (bash) shell behaviour provided in the question answers.
Some really helpful answers have been provided to the original question. In order to short-circuit the expansion behaviour that is explained in the answers, there seems to be at least two ways of preventing a second alias, from trying to expand a command that you have already aliased. For example, if you have alias cmd='cmd --stuff'
which is overriding a native command called cmd
, you can prevent the 'cmd' alias from being used in place of the native cmd
within other aliases, by:
(thanks to wjandrea's comment for this first approach)
- prefixing
cmd
with 'command' in the other alias e.g.alias other-cmd-alias='command cmd --other-stuff'
or,
- Similarly, you can escape aliases (as you can also do on the command line), within other aliases by prefixing with a backslash '', e.g.
alias other-cmd-alias='\cmd --other-stuff'
.
command
inalias lsc='command ls ...'
alias ls='ls some-other-alias'
, but want to use 'ls' the command in it's raw form in a different alias, you can usealias ls2='\ls --funky-other-alias'
and it seems to prevent the shell from expanding 'ls' within the ls2 alias, and just executes plain old 'ls'.