How do I run a shell script without using "sh" or "bash" commands?

Rameez Hussain picture Rameez Hussain · Jan 8, 2012 · Viewed 575.5k times · Source

I have a shell script which I want to run without using the "sh" or "bash" commands. For example:

Instead of: sh script.sh

I want to use: script.sh

How can I do this?

P.S. (i) I don't use shell script much and I tried reading about aliases, but I did not understand how to use them.

(ii) I also read about linking the script with another file in the PATH variables. I am using my university server and I don't have permissions to create a file in those locations.

Answer

fge picture fge · Jan 8, 2012

Add a "shebang" at the top of your file:

#!/bin/bash

And make your file executable (chmod +x script.sh).

Finally, modify your path to add the directory where your script is located:

export PATH=$PATH:/appropriate/directory

(typically, you want $HOME/bin for storing your own scripts)