If we don't have root access, or we only want our script to be accessible to us, the other approach to getting our program in a PATH directory is to make our own bindir.
This can be any directory you like. The system does not require bin to be the name of the directory, but it's a good idea because it will make clear what the directory is for. We'll create a new user bindir in our home directory, called ~/bin.
First, we create the directory with mkdir, and then put hello inside it:
bash$ mkdir /home/bashuser/bin bash$ mv hello /home/bashuser/bin
If we try running hello now, it still doesn't work, because our new bindir is not yet part of PATH. We can do that by adding the directory to the end of PATH with a variable assignment:
bash$ PATH=$PATH:/home/bashuser/bin
With this done, our command now works:
bash$ hello Hello, bashuser!
We can make sure Bash is running the command we expected with a few type checks:
bash$ type -a hello hello is /home/bashuser/bin/hello bash$ type -t hello file bash$ type -p hello /home/bashuser/bin/hello
This shows us that the hello file in our new user bindir is definitely being run when we invoke the hello command. We hope that by now you can see how useful the type command is for understanding what commands are actually doing on your system.
If we want to keep this setup permanently, we will need to make sure that our new directory is added to PATH each time we log in. You can do this by adding a line to your .bash_profile file, or .profile if .bash_profile does not exist:
PATH=$PATH:/home/bashuser/bin
This will append our /home/bashuser/bin bindir to the end of the existing value of PATH on every login, so that any scripts we put in our user bindir will be available to us by any process we run—not just Bash!