A reader writes:
I’ve started to go through your shell script book Wicked Cool Shell Scripts but there is still something I’m unclear on. I’m showing my ignorance here, but how
exactly do you save a shell script so you can use it later?”
If you’re comfortable using the vi editor within the Mac OS X environment, your best bet is to open up a Terminal window (Applications -> Utilities -> Terminal) and type in the script directly.
If not, you can use an application like BBEdit, save the file as text-only with or without line breaks, then open up the Terminal application and type in the name of the script, as in sh myscript.sh (assuming you saved the file as ‘myscript.sh’).
The final step is for you to actually make the script executable, which you can do by using the chmod command, as in: chmod +x myscript.sh.
Once you’ve done that – particularly if you have the script in one of the directories in your PATH – you can execute the script by simply typing its name at the command line.