bash: unable to set and use alias in the same line
Asked Answered
I

2

12

I would expect the second line to say foo instead of command not found:

$ alias foo="echo bac" ; foo;
-bash: foo: command not found
$ foo
bac
$

Why won't the second line say foo? Tested with the following shells, same behavior:

  • bash 3.2.5
  • zsh 5.0.8
  • dash 0.5.9
  • busybox 1.25.0
Illmannered answered 22/7, 2016 at 12:36 Comment(0)
B
10

The behaviour you're seeing is described in the Bash Reference Manual (emphasis mine):

The rules concerning the definition and use of aliases are somewhat confusing. Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias.

Presumably the other shells also behave in this way.

Bertram answered 22/7, 2016 at 13:5 Comment(0)
Y
3

To set and use alias in the same line in bash, you can use:

eval $'alias df5=df\ndf5 -h'

(credits: Hauke Laging's workaround + Kusalananda's workaround).


Explanation of the command:

  • Since "an alias definition appearing on the same line as another command does not take effect until the next line of input is read" according to the bash manual as Tom Fenech's pointed out, we use eval and place a new line between the alias definition and its use.
  • From Kusalananda's workaround:

    "The $'...' is a "C string", and bash would expand the \n within it to a literal newline before passing it to eval.

Yukikoyukio answered 9/5, 2020 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.