11

When I type just $PATH as below, the output starts with -bash: followed by the value of $PATH then at the end it prints : No such directory whereas the output of echo $PATH does not produce that output.
Is the bash's readline involved?

[user1@Server1 ~]$ $PATH
-bash: /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/user1/.local/bin:/home/user1/bin: No such file or directory

When I just do echo $PATH the output is:

/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/user1/.local/bin:/home/user1/bin
Anthon
  • 79,293
RKA
  • 877

4 Answers4

15

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • 1
    understood! I treid below commd [user1@Server1 ~]$ abc:def and the output is below. This makes sense. I still don;t understand teh first word of the output - "-bash" -bash: abc:def: command not found – RKA Sep 19 '17 at 20:09
  • 2
    @RaviKumar The -bash identifies the process that is reporting the error. The - in front of bash is a common way for a shell to say "I'm a login shell". – Kusalananda Sep 19 '17 at 20:46
13

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:

4

$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.

resc
  • 1,224
  • 10
  • 19
Luis Dalo
  • 41
  • 2
-2

This is equivalent to

export myvar="echo test"
$myvar

bash expands myvar and executes the result of variable.

The output in this case is

test

  • When you type in $: export myvar="echo test" followed by return you'll get -bash: $:: command not found – Anthon Sep 24 '17 at 07:32
  • @Anthon The $: may be his prompt. It is not a command then. – peterh Sep 24 '17 at 09:17
  • @peterh Yes I know, and although I realised, but since that is not standard ($ I would expect but $:) that should be more clearly indicated (and formatted). It could also be that those two (or three) lines are the name of an executable program (name embedded with newlines), and executable that prints test to stdout. – Anthon Sep 24 '17 at 09:24