How to excute the bash shell script from any directory

bashshell-script

I have a shell script at /home/joey/hello.sh

Now, I want to execute it anywhere like pwd, cut, sort, grep.

So, I just type hello.sh although I am in another directory.

Best Answer

To execute a script you should make it executable.

 chmod u+x  /home/joey/hello.sh

after you can execute with

 ./hello.sh    # if you are in the same directory
 ~/hello.sh    # if you are in another directory
  hello.sh     # if you put in a directory included in the $PATH

with echo $PATH you can see all the directory included in your path, chose one in which you can write (typically ~/bin) and mv it there

 mv ~/hello.sh ~/bin  # If /home/bin is in your path 

Notes

Related Question