Having read a little of alises, I wanted to try one but I couldn't get it to work. Why does this alias not work?

$ printenv PWD
$ alias testpwd="$PWD"
$ testpwd
-bash: /home/vm: Is a directory

But if I do this with echo, it does work?

$ alias testpwd="echo $PWD"
$ testpwd

I don't understand why the first test didnt work?

  • 3
    $PWD contains the pathname to the current directory; what are you expecting would happen if you run it as a command?
    – ilkkachu
    Dec 7, 2022 at 19:41
  • An alias is (the start of) a command. A directory is not a command, so Bash says "/home/vm: Is a directory".
    – l0b0
    Dec 7, 2022 at 20:14
  • Ah I realise the problem after your comment ilkkachu. Thanks
    – john smith
    Dec 7, 2022 at 21:10
  • ... it should work in bash if you set the autocd shell option Dec 7, 2022 at 21:28
  • It will work if you simply replace $PWD with pwd so that the shell doesn't interpret it as an attempt to run the current working directory as a command and instead, is actually an alias to the actual command: alias testpwd="pwd" Dec 7, 2022 at 23:04

1 Answer 1


Both examples work correctly in that they do what you have told them to do. The alias command defines a word that represents the remainder of the definition:

alias testpwd="echo $PWD"

When you use testpwd as the first word in a command you can consider that the word is replaced with echo $PWD. However, because you wrapped the command expansion in double quotes the value $PWD was evaluated when you executed the command definition. So in this case it was saved as echo /home/vm. Whenever you run testpwd you will get /home/vm output, regardless of your current value for $PWD. (As usual, use 'single' quotes instead of "doubles" to avoid immediate evaluation of variables by the shell.)

The first example works the same way; this defines testpwd to mean "$PWD":

alias testpwd="$PWD"

Now when you run testpwd you can consider (as before) it is replaced by the current value of "$PWD" at the point the alias was defined, in other words /home/vm. This attempts to execute what was the current directory as a command, which will fail miserably:

-bash: /home/vm: Is a directory

You must log in to answer this question.

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