Finding scripts with $PATH

You may have noticed in the previous section that we haven't yet called hello in quite the same way we would another program; we still have to put the ./ prefix first, or specify a full path to it. It doesn't work otherwise:

bash$ ./hello
Hello, bashuser!
bash$ /home/bashuser/hello
Hello, bashuser!
bash$ hello
bash: hello: command not found

What's missing? The answer is that we need to use the special PATH variable to specify where it can find commands such as hello, without needing to specify a full filesystem path to them.

Let's take a look at the value of PATH; your own value may vary:

bash$ declare -p PATH
declare -x PATH="/usr/local/bin:/usr/bin:/bin"

We notice two things:

  • It's an environment variable (-x ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.