$PATH by itself vs echo $PATH

The first word on a simple command line is a command - an action. (There are more complex variants but for now consider this as a sufficient truth.)

In your first example, the "command" is the value of the $PATH variable, which isn't actually a command, so bash complains that it can't find it to run. (The shell searches the colon-separated list of directories specified in the $PATH variable for the command that you've entered.)

In your second example, the "command" is the echo verb, with the value of $PATH as its argument. The echo command prints its arguments to stdout, so you get to see the value of $PATH on the screen.


If you type the command

$ cat food

you’ll get the error message

cat: food: No such file or directory

If you type the command

$ cp abc def

you’ll get the error message

cp: cannot stat ‘abc’: No such file or directory

It’s very very common for error messages in Unix & Linux to begin with the name of the program that issued (i.e., wrote) them.  So, when you type

$ abc:def

into a bash shell, it’s only natural that the error message

-bash: abc:def: command not found

begins with the name bash, because bash issued that message.  The one part that’s a little tricky is that it says -bash instead of bash.  This occurs because bash is a shell, and specifically, a login shell.  By convention, the names of login shells always begin with a -.

For more background information on this, see:

  • What is the exact difference between a ‘terminal’, a ‘shell’, a ‘tty’ and a ‘console’?
  • Difference between Login Shell and Non-Login Shell?
  • WHY a login shell over a non-login shell?
  • What is the difference between interactive shells, login shells, non-login shell and their use cases?
  • .profile and .bash_profile ignored when starting tmux from .bashrc?
  • What does exec $SHELL -l do?

$PATH just evaluates the variable and tries to run that as a command, since there are no arguments nor actual command name, then it complains as: no such file or directory.

echo $PATH is explicitly giving a command to display the contents of the $PATH variable.

Tags:

Bash