Mac OS/X Bash Shell

Most Mac users do not have an interest in the Unix shell (running in Terminal). The Mac was carefully an exhaustively developed so an Apple user would not require it. The question is, could a more advanced user enjoy using the shell? I think, yes.

Most things are done very efficiently using the Mac’s graphical interface. Searching for files, opening them, copying them…all easy and efficient. Spotlight works great. But, what if you want to do something with every file found in a search? That is an example of when good old Unix shell commands or shell scripts (little programs written to run on the Bash shell) are extremely useful.

How to Open Terminal

Terminal is the program that the Unix shell runs in. To open Terminal, use Spotlight. Type command-spacebar (hold down the command key and press the spacebar) and enter “terminal” in Spotlight. Select Terminal from the results list. Note that the command key is the one that says “command” and has a cloverleaf looking icon – it is next to the spacebar.

Or, use Finder and locate Terminal in the directory: /Applications/Utilities/

Groundwork

Current Macs use a version of the Bash shell. This is the most common type of Unix shell. Most of what works generally in common versions of Unix, works on the Mac. So, when you want to use Google to find a command, or how to use a command, adding the word “unix” usually helps. If you are going to use less common parameters, make sure to also check out specific OS/X reference pages, because there are differences from one Unix to another.

Case is important. Filenames, directories…just about everything is case sensitive. Therefore, you could have “photos” and “Photos” in the same directory. “ls” is the list files command. “LS” and “Ls” are not!

Switches: In Unix, switches are demarcated with a hyphen (“-“). In Windows/DOS a slash character is used (“/”). When more than one switch is selected, they can be strung together after the hyphen. For instance, with the “ls” (list files) command, if you wanted to use both the “-a” (all files, including hidden files) and “-l” (long listing format) switches, the command line would be:

ls -al

Globing: Globing is the term for Unix-style “wildcards”. These are handled completely differently than they are in Windows/DOS. In Windows, the program being executed is responsible for expanding wildcard characters. In Unix, the shell expands the filename specified. The Unix globing characters are:

? Matches any one character in a filename.
* Matches any character or characters in a filename.
[ ] Matches a character within the brackets.
- A hyphen used within [ ] denotes a range of characters.
~ A tilda at the beginning of a filename expands to be the user’s home directory.

Note the absence of a “.” period character. Unix does not have the equivalent of Windows-style filename extensions.

A Starter Set of Commands

Here are a few shell commands to start with.

ls List files. This is sort of like DIR in DOS.
cd Change directory.
mkdir Make directory.
rmdir Remove directory.
cat Concatenate file(s) to standard output. Can be used to display file contents.

Jump in and get your feet wet. Next, I’ll revisit this topic and expand on it a bit.

Be Sociable, Share!