Shell script example for OS X

Almost any series of commands which can be run in the shell can be run from a shell script. Tying in some of the last few posts I published here this is an example.

Let’s say I wanted to run the gedit editor from the shell rather than from the dock/GUI… I would first have to locate the executable program. I happen to know it is installed in “/Applications/“. By running this program the gedit editor will start. Since it was intended to be started from a GUI there is some whining by the program itself, but it works.

The next thing is to put the command into a file. There are several ways to do this. The editors I discussed in an earlier article will do. There are also a number of UNIX tricks to do the same thing without using an editor. Each is equally valid as long as the task is done. In my case I used pico and stored the file in my bin directory under the name gedit.

Now for a bit of UNIX magic… A file is not a program until the operating system knows it is a program. In this case typing “chmod 755 gedit” will do the trick. (Geek stuff) I used 755 instead of 700 because I wasn’t sure if 700 would work. By typing gedit from the command line the gedit editor will now run (as long as the script location is in the $PATH).

Lastly, there is a bit of icing on the cake… If I wanted to edit a particular file it would be nice to be able to specify it. As it is right now the script will only start the editor but not open or edit a particular file. The trick here is to go to the end of the line and add “ %1“. Note there is a space before the percentage sign. This will pass along the name of the file to the original program. After saving the file everything works as planned. As an added bonus there is no need to perform another chmod on the file.

Please follow and like us:

About the Author


Mike Pfaiffer was President of A.P.P.L.E. and also the president of Digital Civilization magazine, a monthly UNIX magaine. Mike wrote a number of articles for A.P.P.L.E. and sadly passed away 19 July 2013 at age 54.