Quick Guide to Bash Shell Scripting

Oct 30, 2009 - 5 Comments

mac terminal Shell scripting is wildly useful and a powerful way to manipulate a lot of files and to automate behind-the-scenes tasks in Mac OS X. This guide is intended for Linux users but the Bash shell is also used in Mac OS X, and as far as I can tell everything in this bash shell scripting guide works the same across the two platforms. Before checking out the guide you might want to check what shell you are using in Mac OS X by typing the following in the Mac Terminal:

echo $SHELL

Something like /bin/bash or /bin/tcsh will be reported back to you, obviously this Bash scripting guide only works for the Bash shell. If you need to, you can change the default shell rather easily in the Terminal preferences, but Bash has been the default shell since 10.3 and continues to be in Snow Leopard.

LinuxHelp: 10 second guide to Bash Shell Scripting

If you’re looking for more, a much more advanced and complete guide to Bash scripting is available here:

Advanced Bash Scripting Guide

And if you really want to dive into shell scripting, a good book like Pro Bash Programming: Scripting the GNU/Linux Shell will do you wonders.

.

Related articles:

Posted by: Bill Ellis in Command Line, Development, Mac OS, Tips & Tricks

5 Comments

» Comments RSS Feed

  1. Starkoman says:

    This nonsense makes me sick.

    The whole point of the Apple GUI was to avoid this stuff.

    Explain, please, how learning Terminal/UNIX/Linux commands are required — and then explain (please) why one has to learn this very difficult stuff instead of providing a nice GUI.

    Most of us chose Apple in previous decades because we could work with design and pre-press rather than get involved in computing. A little help would be much appreciated. Thank you all.

    • Johan says:

      To Starkoman:

      The average user will probably never feel the need to learn to author their own scripts. How ever, the average user most often don’t run into situations where one has to do tedious tasks for which there isn’t a GUI.

      As a linux newbie, I hated the terminal, I didn’t understand any command. But then it happened, I had a repetitive task of transcoding video files and adding intros to them for my youtube channel. I spent countless hours on this tiresome task every week before I finally started looking at scripting.

      Nowadays, I record my videos(multiple episodes), put them into individual folders, open up a terminal and cd to the correct directory, typ “Script-A.sh” and a couple of hours later they are ready for upload to my youtube-channel.

      Another example is when I saw my college take on the task of manually converting about 1000 .pic pictures to .jpg with photoshop.

      I spent about 20 minutes to research a suitable batch tool for this (with a GUI), the one’s I found lacked the recruitments he needed. Then I found a command line tool, so I started scripting, another 20 minutes later all pictures had been converted. At that time, he had already spent like 2 hours and had only done about 1/10 of the images.

      Most people will probably give up when facing these kinds of tasks (if it doesn’t “just work” it ain’t worth doing, right?). Scripting could in many (at least in my) cases take hours of research and trial and errors before one gets a workable solution… but under some circumstances, it is usually beneficial in the long run.

    • Polo1 says:

      What a stupid comment. No-one’s forcing you to use the terminal.
      ‘Getting under the hood’ is however often a very efficient way to do tasks in OSX. If you only use your computer for its apps, you need never go near the terminal – it’s not for numpties. But a terminal interface provides extremely powerful facilities that for many people are well worth the effort.

  2. Woodgie says:

    Not to mention the O’Reilly book “Learning the Bash shell” by Cameron Newham.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site