6

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
  • 2
    $PATH tries to run your PATH as a command, echo $PATH outputs your PATH. – DisplayName Sep 19 '17 at 19:48
  • 2
    I'm curious, what did you want $PATH to do? – Kusalananda Sep 19 '17 at 21:13
  • 2
    Look at the end of the output, not only the beginning: No such file or directory. Bash is not finding the /usr/local/bin:/usr/bin[...] executable, and hence the error. – Jorge Bellon Sep 20 '17 at 7:48
11

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.

  • 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 – Ravi Kumar 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
11

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:

3

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

-3

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 7:32
  • @Anthon The $: may be his prompt. It is not a command then. – peterh Sep 24 '17 at 9: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 9:24

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.