1

I noticed an example from https://unix.stackexchange.com/a/383825/674

$ alias foo=bar
$ foo () { blah "$@"; }
$ type -a foo bar  
foo is aliased to `bar'
bar is a function
bar ()
{
    blah "$@"
}

So redefining the alias foo actually redefines the aliased command bar. This works like a nameref, i.e. a variable with the reference attribute.

I experimented more with the following examples.

  • Why does mya=cat not re-alias mya to cat, nor redefines the aliased echo to cat?

  • Why does mya () { cat test.sh; } redefine the aliased echo to the function, just like a nameref?

Thanks.

$ alias mya=echo
$ type mya
mya is aliased to `echo'
$ mya abc  # mya behaves exactly as echo
abc

$ mya=cat
$ type mya
mya is aliased to `echo'
$ mya test.sh # mya=cat doesn't alias mya to cat
test.sh


$ mya () { cat test.sh; }
$ type mya
mya is aliased to `echo'
$ mya  # Redefining mya as a function works, by outputing the content of test.sh
#! /usr/bin/env bash
echo $_
echo $0
$ echo # Redefining mya also redefines the aliased echo, just like a nameref
#! /usr/bin/env bash
echo $_
echo $0
2
  • mya=cat defines a variable, not an alias. You'd have to use $mya to access the variable's value.
    – n.st
    Commented Aug 4, 2017 at 16:04
  • And another data point for you: alias foo=bar; function foo () { echo booh } → The alias foo shadows the function foo (like it would for normal programs, see the ubiquitous alias rm='rm -i'), but as usual you can circumvent the alias by calling \foo.
    – n.st
    Commented Aug 4, 2017 at 16:07

1 Answer 1

2

An alias is expanded when it's the first word in the command. So when you type:

alias foo=bar
foo () { blah "$@"; }

the alias foo is expanded, so it's treated as if you'd typed:

bar () { blah "$@"; }

When you type:

alias mya=echo
mya=cat

the first word in the command is mya=cat, not just mya, so the alias is not expanded. = is not a word delimiter, it's merely the delimiter between the variable and value in a variable assignment.

5
  • Thanks. How can i realias an existing alias to a different command, since assignment doesn't do the work?
    – Tim
    Commented Aug 5, 2017 at 10:00
  • Just do another alias command. alias mya=cat
    – Barmar
    Commented Aug 5, 2017 at 16:23
  • Variable assignment is not subject to alias substitution, but this is not because the first word of the command is mya=cat. The first word of the command is what follows all variable assignments (in your example there is none). Therefore mya=cat foo=bar mya ... would execute echo ...
    – xhienne
    Commented Aug 6, 2017 at 1:14
  • @xhienne True, that's another way to explain it. What I find somewhat confusing is that alias substitution is only supposed to happen in simple commands. Is a function definition a simple command? Yet it's happening there.
    – Barmar
    Commented Aug 6, 2017 at 1:27
  • Each compound command is split in simple commands. Each first word of those simple commands is subject to alias substitution. A function is a compound command.
    – xhienne
    Commented Aug 6, 2017 at 2:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .