TweetFollow Us on Twitter

Mac in the Shell: Customizing the bash experience

Volume Number: 24 (2008)
Issue Number: 09
Column Tag: Shell

Mac in the Shell: Customizing the bash experience

Get comfortable in the bash shell and make the experience your own

by Edward Marczak


I've written about bash for some time. Mac in the Shell has covered variables, loops and helpful OS X specific command-line utilities. We've even covered and ways to make it work the way you want. We haven't, though, covered customizing the bash shell itself. Not comprehensively, at any rate. There have been snippets here and there over time, but not in one Mac in the Shell article. It's time to rectify that. Follow along and get comfortable in the bash shell.

Back to the Beginning

For those of you that are new to this, why are "command-line people" so passionate about working the way they do? Why is the command-line interface (CLI) better then a graphical user interface (GUI)? Here's a quick recap:

Speed: even on the slowest Machines that can run Mac OS X, working in a shell will be practically as fast running on the fastest machine. The text-based interface is built for speed.

Nothing is hidden: Mac OS X's primary interface to the operating system, The Finder, hides certain aspects of the underlying system from the end user. I'd argue that this is perfectly the right decision for the bulk of end-users. However, you're a tech - you're reading MacTech, right? - and you need to know what's really going on.

Consistency: despite all of the changes that graphical interfaces have gone through over time, the text-based command line interface has really remained the same.

In short, there's room for both a GUI and a text-based shell, as they both have pros and cons. It's all about choosing the right tool for the job. Many people not familiar with a shell, though, tend to not really want to explore it as it is so foreign from the GUI. Just as many people are completely comfortable closing a window by pressing command-w, you can learn the keyboard shortcuts to maneuver about the command-line. It just takes practice. Let's wade in slowly.

Two things to note before going further: First, this article isn't meant to be a comprehensive guide to bash. Rather, its aim is to teach you things that will get you comfortable with the shell. Secondly, none of this is set in stone. I know many people whose primary environment is the command line. Each of them has preferred tricks and shell usage that suits them. Try each of the tips presented in this article and see which ones bind themselves to you.

Wading In

This article assumes two things: You're running Mac OS X v 10.5 "Leopard" as your operating system, and that bash is your primary shell. Everything described in this article will work just fine on 10.4 "Tiger," however, there may be slight discrepancies in terminal output. "Tiger" uses bash version 2.5 as its primary shell, and "Leopard" uses bash version 3.0, so, as you go deeper, Leopard has the more capable version of bash by default.

First thing's first: Open is your gateway to the shell (in this case, bash). is located in Applications > Utilities. Open it now, and keep it in your Dock, because after this article, you're always going to use it! Once it's open, you'll see a small-ish white window, with text similar to the following:

Last login: Thu Jul  9 09:40:51 on console
jack-kerouac:~ marczak$ 

The bash shell prints the first line. You're told about the last time this account was logged in, and from where. "console" represents a login from (or other terminal program, such as iTerm or Terminator) via the GUI directly on the machine. A login via ssh would display the ip address of the machine that the remote session connected from. The following line is the prompt. As shown, the default prompt set by Apple lists the following attributes: hostname ("jack-kerouac"), the current directory ("~") and user name ("marczak"), all followed by a dollar sign ("$"). The dollar sign denotes the current effective user ID. Shortly, we'll talk about customizing the prompt. More importantly, though, don't look at this and be frightened off!

The tilde character represents your home directory. In the example prompt above, the current working directory is the home of user marczak. If you're rusty on basic shell commands, see the earlier Mac in the Shell column, "The Shell: What?," on-line at Just a quick example of how the prompt can be dynamic, change the current directory using the cd command:

jack-kerouac:~ marczak$ cd Library
jack-kerouac:~/Library marczak$

The prompt that you see is up to you. bash determines what to display by the contents of the variable $PS1. Let's see how the current prompt is defined:

$ echo $PS1
\h:\W \u\$ 

True to the description above, each backslash escaped substitution character displays a full value when actually at the prompt. Apple's default uses these characters:

\h - the current host name.

\W - the basename of the current working directory (with $HOME abbreviated as a tilde).

\u - the current username.

\$ - if the effective UID is 0, a #, otherwise a $.

(space) - you can't see it in print, but there is a space at the end of that sequence. This nicely separates the "$" or "#" character from the cursor and your typing.

Other characters in the string are taken literally, such as the colon (":") in "\h:\W \u\$". If PS1 is not set ("undefined"), bash uses "\s-\v\$ ". In other words:

\s - the basename of the current shell.

\v - the version of the current shell.

Which, on a 10.5.4 machine looks like this: "-bash-3.2$". See the "Prompting" section in the bash man page for a complete list of escape sequences. I've been setting my prompt like this for some time now:

"\u@\h\n[ \w ]$ "

This produces a two-line prompt, thanks to the newline character ("\n") embedded in the string. My prompt looks something like this:

[ ~/download ]$

The user name ("marczak") and hostname ("lists") are prominently separated on one line, while the full current working directory ("~/download") precedes the input line. This prompt has been a huge help, as I have roughly 12 or more shells open at any given time, and tend to ssh into other systems often. This prompt is consistent across all machines I work with and allows me to quickly identify my UID, the system I'm on, and what directory I'm working in.

Editing Startup Files

If you've followed along up to this point in the article, you may have a prompt that you're very happy with. One that you want to show up each time you start up a shell. If that's the case, you need to add your definition of PS1 to a bash startup file. bash makes a distinction regarding a "login shell" and a "non-login shell." A login shell is created when starting up an initial shell. This occurs when starting, or, when accessing a remote machine via ssh. A non-login shell is a subshell. By default, bash runs the commands (or, "sources") in the following files every time a bash login-shell is created:

/etc/profile - the system-wide initialization file. All accounts logging in source this file.

One of: ~/.bash_profile, ~/.bash_login, and ~/.profile - whichever is found first is used.

A non-login shell skips all of the aforementioned files. It does have an initialization file, though: ~/.bashrc. This allows you to logically separate commands needed for login and non-login shells. If you peek in /etc/profile, you'll notice that Apple has included a command to also call /etc/bashrc for non-login shells. This is a slightly non-standard practice. The bash man page makes no reference to a system-wide /etc/bashrc file for non-login shells. Personally, I tend to not rely on non-standard setups like this. bash exists on other platforms and I like to keep practices that work across any system I'm using.

Consider this the "easy version" of bash initialization files. There are some other subtleties that you needn't be concerned with at this stage. For the curious, though, it's all in the bash man page.

To round this out, I keep the following line in my ~/.bash_profile file:

export PS1="\u@\h\n[ \w ]$ "

This way, I see the same prompt whenever I'm in a shell, even after a reboot.

Moving Around

Another thing that tends to frustrate people new to the shell is ease of moving around on the command line. In GUI apps, a point and click of the mouse sets the cursor (insert point) where you want it. This is almost possible in the shell, too. Apple's allows an option-click to place the cursor (this is an option in 10.4 that needs to be explicitly set). However, when at the keyboard, the less you reach for the mouse, the faster you can navigate.

By default, your shell is in emacs editing mode. Essentially, the keystrokes used to maneuver about the command-line mimic those used by emacs. Again, my aim here is to keep this tutorial simple and digestible. Starting out with these key commands will allow you greater flexibility initially, and you can then add more advanced commands into your repertoire.

Apple has mapped as many keys to sane equivalents as possible. Here's a list of the very obvious key commands that should put you right at ease:

left arrow, right arrow: position cursor on input line.

Delete, forward-delete: delete a character to the left, or beneath the cursor

As you type your input, use the left and right arrow keys to reposition the cursor one place to the left or right, respectively. The delete key behaves as expected; deleting the character to the left of the cursor and moving all of the text to the right of the cursor back one space. The forward delete also acts appropriately and deletes the character under the cursor while moving all text to the right of the cursor back one space.

While these key commands are certainly workable, they can get a little cumbersome when working with long lines of text.

ctrl-a, ctrl-e: move to beginning, end of input line, respectively.

option-f, option-b: move forward, backward by a word, respectively.

ctrl-k: kill from the cursor to the end of the input line.

option-d, ctrl-w: delete from the cursor to the next word or previous whitespace, respectively.

Try those, one at a time and get used to them. These commands come in most handy when working on a command-line that has a lot of text (a long line), but will work with any length line. Perhaps the easiest to get used to is ctrl-a and ctrl-e for jumping to the beginning of a line or to the end of the current line. If you use screen (see MacTech's article "Screen: Living in a Virtual World" at if you need an intro or refresher), you'll notice that ctrl-a interferes with the binding for screen commands. You can re-bind your screen key elsewhere, but as always, I like to keep apps using their default keys as much as possible. This simply adds an extra keystroke if you use screen; the command becomes ctrl-a, a (control-a, then 'a' on its own).

Shortening Commands

Another way to make your life on the command-line easier is to define aliases. Aliases are substitute commands that you define. Best way to demonstrate this is an example:

$ alias tsl="sudo tail -f /var/log/system.log"

This defines the alias "tsl" as "sudo tail -f /var/log/system.log". With this alias in place, simply typing "tsl" on the command line and pressing return will actually run "sudo tail -f /var/log/system.log". If there's some long, convoluted command that you find yourself typing often, consider making an alias to it to make your life a little easier, and lessen the number of keystrokes required to achieve your goal.

Like anything that you define in a single shell by typing it at the command-line, it's not going to persist into a new session. Once you start defining aliases, the ones that you want to be available to new shells need to be defined at shell login time (or, you can type them by hand each time...). Again, this is a perfect use for your shell initialization files. Anything placed in ~/.bash_profile will only affect your user account when logging in, or, you can place generally useful aliases in /etc/profile to affect all accounts that start a shell.

bash aliases are incredibly useful, but there are some quirks; nothing that you'll run into when just starting out, but things to be careful of later on. For example, aliases are evaluated after bash reads the current line completely. This causes a problem for an alias placed in the middle of a line that's expecting substitution:

$ files=$(ls -l /home/lists/) ; prf ; killall myproc

...where prf is an alias to some string. This will fail in some not so nice ways. For all intents and purposes, bash functions can do all that aliases can and more. Further, they behave as you'd expect any other command to behave.

Functions are defined with the function command. Here is a quick example script that uses a bash function that accepts a parameter:

Listing 1:

function is_local_user() {
  dscl . -read /Users/${1} &> /dev/null
  if [ $? -ne 0 ]; then
    echo "Not a local user"
    exit 1
    echo "User is local"
is_local_user ${USER}
ls -l

While this is a completely contrived example, it illustrates several points. First, functions are defined with the function keyword and curly braces block the definition itself. Secondly, it can span multiple lines and contain flow control (if/then or for loops), which aliases cannot. Finally, bash functions can be defined in bash initialization scripts and be available at the command prompt, just like an alias. If you wanted a quick command that did something particular based on some state - a file's status, the day of the week, and so on - the task could be defined as a bash function.

Of course, it could also be defined as a stand-alone script. Functions have an advantage, though. bash functions are evaluated in the current shell context. In other words, unlike a stand-alone script, bash does not fork off a new process to evaluate a function. That's nicer all around. Less processes running, and less context switching for the operating system. Bravo. The simple rule is this: if it's a short enough function that is used frequently, define it as a bash function in a bash initialization file.

Seeping In

It's probably about right to let all of that seep in. Work with all of the information presented here. Practice. A little at a time. I was hoping to get even a little further, but this is certainly enough to get started. Next month we'll go into more ways to make working in the shell easier.

The bottom line is this: no matter if you create applications in C, Python, Perl, Ruby or other language, you'll most likely need to interact with a shell and command-line applications in some manner. The shell is your interface to running these other utilities and programs you create. To run your Python application, you'll most likely interact with bash, as it's now the default shell. The more efficient you are with the shell, the more efficient you can be overall.

Media of the month: my one-month absence from writing Mac in the Shell came from having too many other priorities. One of which was as Executive Editor of this magazine to get the issue set and out the door! Another was my family. And another was writing one of the reference guides for Apple's ACSA Certification and course. In fact, three books and courses were created in parallel, each given topic perhaps nearer to your needs than the other. They're all deep pieces of work that each author worked incredibly hard on. I can't single one out, so, check out the title you wish. They're all part of the "Apple Training Series:"

Directory Services, by Arek Dreyer

Deployment, by Kevin White

Advanced System Administration, by Edward Marczak

...or check out all three! (Which you'll need to do to gain the 10.5 ACSA Certification). All have been written with the latest version of OS X - 10.5 at this writing.

Until next month, stay in practice!

Ed Marczak is the Executive Editor for MacTech Magazine, and has been lucky enough to have ridden the computing and technology wave from early on. From teletype computing to MVS to Netware to modern OS X, his interest was piqued. He has also been fortunate enough to come into contact with some of the best minds in the business. Ed spends his non-compute time with his wife and two daughters.


Community Search:
MacTech Search:

Software Updates via MacUpdate

Civilization VI 1.0.0 - Next iteration o...
Sid Meier’s Civilization VI is the next entry in the popular Civilization franchise. Originally created by legendary game designer Sid Meier, Civilization is a strategy game in which you attempt to... Read more
Paperless 2.3.7 - $49.95
Paperless is a digital documents manager. Remember when everyone talked about how we would soon be a paperless society? Now it seems like we use paper more than ever. Let's face it - we need and we... Read more
Apple iMovie 10.1.3 - Edit personal vide...
With an all-new design, Apple iMovie lets you enjoy your videos like never before. Browse your clips more easily, instantly share your favorite moments, and create beautiful HD movies and Hollywood-... Read more
Apple Numbers 4.0.5 - Apple's sprea...
With Apple Numbers, sophisticated spreadsheets are just the start. The whole sheet is your canvas. Just add dramatic interactive charts, tables, and images that paint a revealing picture of your data... Read more
Xcode 8.1 - Integrated development envir...
Xcode includes everything developers need to create great applications for Mac, iPhone, iPad, and Apple Watch. Xcode provides developers a unified workflow for user interface design, coding, testing... Read more
iShowU Instant 1.1.0 - Full-featured scr...
iShowU Instant gives you real-time screen recording like you've never seen before! It is the fastest, most feature-filled real-time screen capture tool from shinywhitebox yet. All of the features you... Read more
RestoreMeNot 2.0.4 - Disable window rest...
RestoreMeNot provides a simple way to disable the window restoration for individual applications so that you can fine-tune this behavior to suit your needs. Please note that RestoreMeNot is designed... Read more
DEVONthink Pro 2.9.6 - Knowledge base, i...
DEVONthink Pro is your essential assistant for today's world, where almost everything is digital. From shopping receipts to important research papers, your life often fills your hard drive in the... Read more
Apple Pages 6.0.5 - Apple's word pr...
Apple Pages is a powerful word processor that gives you everything you need to create documents that look beautiful. And read beautifully. It lets you work seamlessly between Mac and iOS devices, and... Read more
Safari Technology Preview 10.1 - The new...
Safari Technology Preview contains the most recent additions and improvements to WebKit and the latest advances in Safari web technologies. And once installed, you will receive notifications of... Read more

Latest Forum Discussions

See All

The 4 best food delivery apps
As the temperatures continue to drop, so does the motivation to venture outside. Sometimes you still want to eat a nice meal from that sushi place down the road though. Thankfully in these trying times, there are a number of fine food delivery... | Read more »
Toca Life: Farm (Education)
Toca Life: Farm 1.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0 (iTunes) Description: Work and play the farmer's way! Milk your cow, gather eggs from your hens and raise your crops. Have a picnic, play the... | Read more »
The Lost Shield (Games)
The Lost Shield 1.0.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0.0 (iTunes) Description: The Lost shield is a brick break/adventure game. You play as a hero who must return a powerful but dangerous magic shield... | Read more »
The Forgotten Room (Games)
The Forgotten Room 1.0.1 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0.1 (iTunes) Description: Play as paranormal investigator John “Buster of Ghosts” Murr as he explores yet another mysteriously creepy house. This... | Read more »
5 Halloween mobile games for wimps
If you're anything like me, horror games are a great way to have nightly nightmares for the next decade or three. They're off limits, but perhaps you want to get in on the Halloween celebrations in some way. Fortunately not all Halloween themed... | Read more »
The 5 scariest mobile games
It's the most wonderful time of the year for people who enjoy scaring themselves silly with haunted houses, movies, video games, and what have you. Mobile might not be the first platform you'd turn to for quality scares, but rest assured there are... | Read more »
Lifeline: Flatline (Games)
Lifeline: Flatline 1.0.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0.0 (iTunes) Description: The Lifeline series takes a terrifying turn in this interactive horror experience. Every decision you make could help... | Read more »
Game of Dice is now available on Faceboo...
After celebrating its anniversary in style with a brand new update, there’s even more excitement in store for Game of Dice has after just being launched on Facebook Gameroom. A relatively new platform, Facebook Gameroom has been designed for PC... | Read more »
4 addictive clicker games like Best Fien...
Clickers are passive games that take advantage of basic human psychology to suck you in, and they're totally unashamed of that. As long as you're aware that this game has been created to take hold of your brain and leave you perfectly content to... | Read more »
Smile Inc. Guide: How not to die on the...
As if Mondays weren't bad enough, at Smile Inc. you have to deal with giant killer donuts, massive hungry staplers, and blasting zones. It's not exactly a happy, thriving work environment. In fact, you'll be lucky to survive the nine to five.... | Read more »

Price Scanner via

Apple Unveils Redesigned MacBook Pro With Tou...
October 27, 2016 – Apple today introduced the thinnest and lightest MacBook Pro yet, along with a new interface innovation that replaces the traditional row of function keys with a Retina-quality... Read more
Apple Unveils New TV App for Apple TV, iPhone...
October 27, 2016 – Apple today introduced a new TV app, offering a unified experience for discovering and accessing TV shows and movies from multiple apps on Apple TV, iPhone and iPad. The TV app... Read more
Price drops on select refurbished 2015 13″ Re...
Apple dropped prices on select Certified Refurbished 2015 13″ Retina MacBook Pros by as much as $90. An Apple one-year warranty is included with each model, and shipping is free: - 13″ 2.7GHz/256GB... Read more
Apple reveals new next-generation 15″ and 13″...
Apple today revealed their next-generation 15″ and 13″ MacBook Pros. The new models are thinner and lighter than before with a new aluminum design featuring an enhanced keyboard with retina, multi-... Read more
Worldwide Smartphone Shipments Up 1.0% Year o...
According to preliminary results from the International Data Corporation (IDC) Worldwide Quarterly Mobile Phone Tracker, vendors shipped a total of 362.9 million smartphones worldwide in the third... Read more
TuneBand Arm Band For iPhone 7 and 7 Plus Rel...
Grantwood Technology has added the TuneBand for iPhone 7 and 7 Plus to its smartphone armband series. The TuneBand provides a lightweight and comfortable way to wear the iPhone while running,... Read more
1.4GHz Mac mini on sale for $449, save $50
Adorama has the 1.4GHz Mac mini on sale for $50 off MSRP including free shipping plus NY & NJ sales tax only: - 1.4GHz Mac mini (Apple sku# MGEM2LL/A): $449 $50 off MSRP To purchase a mini at... Read more
21-inch 1.6GHz iMac on sale for $999, save $1...
B&H has the 21″ 1.6GHz Apple iMac on sale for $999 including free shipping plus NY sales tax only. Their price is $100 off MSRP. Read more
Macs’ Superior Enterprise Deployment Cost Eco...
IBM’s debunking of conventional wisdom and popular mythology about the relative cost of using Apple Mac computers as opposed to PCs running Microsoft Windows at the sixth annual Jamf Nation User... Read more
12-inch WiFi Apple iPad Pros on sale for $50-...
B&H Photo has 12″ WiFi Apple iPad Pros on sale for $50-$70 off MSRP, each including free shipping. B&H charges sales tax in NY only: - 12″ Space Gray 32GB WiFi iPad Pro: $749 $50 off MSRP -... Read more

Jobs Board

*Apple* Retail - Multiple Positions - Apple,...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Software Engineering Intern: UI Applications...
Job Summary Apple is currently seeking enthusiastic interns who can work full-time for a minimum of 12-weeks between Fall 2015 and Summer 2016. Our software Read more
Security Data Analyst - *Apple* Information...
…data sources need to be collected to allow Information Security to better protect Apple employees and customers from a wide range of threats.Act as the subject Read more
*Apple* Retail - Multiple Positions - Apple,...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
*Apple* Solutions Consultant - Apple (United...
# Apple Solutions Consultant Job Number: 52812872 Houston, Texas, United States Posted: Oct. 18, 2016 Weekly Hours: 40.00 **Job Summary** As an Apple Solutions Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.