Making a script exectuable

Hi, I'm pretty new to Unix and I just have a question concerning making a script executable without putting the "sh" command before it. In case it makes the difference I am on an Apple computer using the Terminal. Anyway here is the little test code I wrote followed by the commands I took to try to make it executable on its own.

Script
#! /bin/sh
# comments
echo "blah"

heh that's it, it works when I do enter sh Script
now then the code I tried to use to execute it

chmod u+x Script
pwd ; echo $PATH

and though no errors came up, it didn't work. So any insight would be really appreciated. Thanks.

chmod u+x Script
just makes the script executable. It does not execute it.

pwd ; echo $PATH
this displays the current working directory and then it displays the value of PATH

To run the script try:
./Script

well perhaps I'm just confused and expecting a greater return than possible. I expected after running the chmod I would be able to run the script (by typing the script name or ./script) from anyplace as I would other commands such as ls or mv, however at the moment all I can do is run the script through placing it, ie ./folder/folder/folder/script, which seems terribly inconvenient, maybe you can help just by clearing this confusion up for me.

./Script is a path to the script. To run the script without specifying a path, you need to put Script in one of the directories in your PATH environment variable. Typically you would add some directory of your own to your PATH. This is basic stuff and you should read a unix tutorial. Go to our FAQ section and look at the tutorial thread. There are links to a lot of titorials there.

Hi,

Your question is not that clear ... Can u make it more elaborate ...

Anyway I am writing this based on whatever I cud understand from the qn.

chmod just sets the permissions for a file or directory .... If we need to execute the script, we have to do it explicitly ... :slight_smile: either as "sh scriptname" or just "scriptname" ... and the absolute or relative path too if u r not running the script from the directory where the script is kept ....

If your concern is that you have a script somewhere and u want to execute it from any other directory without taking the pain of giving the entire path and running each time ... can try this ....

Edit your .profile file in your home directory ... and put an alias for this script ..

runfromanywhere=/dir1/dir2/dir3/scriptname

now u can just give runfromanywhere and execute ur script from anywhere ... hope i answered ur qn ..

just copy the file in the /usr/bin directory and make it executable...
but u'll need root's login for that...
and be careful not to disturb other files in that directory