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
  • mya=cat defines a variable, not an alias. You'd have to use $mya to access the variable's value. – n.st Aug 4 '17 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 Aug 4 '17 at 16:07
3

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.

  • Thanks. How can i realias an existing alias to a different command, since assignment doesn't do the work? – Tim Aug 5 '17 at 10:00
  • Just do another alias command. alias mya=cat – Barmar Aug 5 '17 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 Aug 6 '17 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 Aug 6 '17 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 Aug 6 '17 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.