executing shell script without calling sh implicitly

ShoX picture ShoX · Jun 8, 2010 · Viewed 25.2k times · Source

I was wondering if it is possible to make a "link" in usr/bin (i.e.) that leads to a shell-script.

But I want just to write

% shellscript

instead of

% sh shellscript.sh

kinda like an alias.

Is this possible?

Answer

rjmunro picture rjmunro · Jun 8, 2010

Make the first line of the script

#!/bin/sh

Then make it executable by typing the command:

chmod +x shellscript.sh

If you now place the script in a bin folder that is on your system's PATH variable and you will be able to run it directly. To see the folders in your path, type:

echo $PATH

I usually use /home/[my username]/bin for scripts that I have written so that they don't interfere with other users on the system. If I want them to be for all users, I use /usr/local/bin which is supplied empty on most distributions.

The .sh on the end of the script's filename is only a convention to help you remember what kind of file it is. It will still work if you rename it to just shellscript, for example, which will complete your requirements.