How to Resolve Bash error: command not found

  • by

I do have an executable file in the present working directory ($PWD).

[edchen@test ~]$ ll
total 4
-rwxr--r-- 1 edchen edchen 21 Mar 12 12:45 say_hello.sh

I know I can execute it with the relative path.

[edchen@test ~]$ ./say_hello.sh
Hello, World!

./ indicates that the execute file is in the present working directory.

Or execute it with the absolute path.

[edchen@test ~]$ /home/edchen/say_hello.sh
Hello, World!

-bash: command not found

But I can't execute it without any indication.

[edchen@test ~]$ say_hello.sh
-bash: say_hello.sh: command not found

This time, we saw error command not found instead.

Solution

To make the shell know what we want to do, we should tell it in advance.

[edchen@test ~]$ vi .bash_profile
...
export PATH=$PATH:$HOME

We add our $HOME to be one of discover-able paths of executable files.

Then we take it effect immediately.

[edchen@test ~]$ . ~/.bash_profile

We used a dot command to source all environment variables in the profile.

Then we do it again.

[edchen@test ~]$ say_hello.sh
Hello, World!

Good, this is what we want. In fact, the customized file can be executed in any directory without modifiers.

Leave a Reply

Your email address will not be published. Required fields are marked *