Customize the root shell's prompt in Terminal

By Rob Griffiths, Macworld |  Software, Mac OS, Unix Add a new comment

Today's hint is for those of you who, like me, use Terminal often and occasionally have a need to do things there as the root user. If you don't know what Terminal is, don't know what the root user is, or haven't ever heard of the sudo command, this hint probably isn't for you.

In Terminal, you enter root mode with the sudo command; in particular, if you've got a lot to do as root, it's easiest to open a root shell with sudo -s. You then stay in root mode until you type exit, whereupon you revert to your "normal" admin-level powers.

There's just one problem with this, though--the default root command prompt isn't nearly scary enough to remind you of the power you're wielding while in this mode.

When running in root mode, the default prompt of bash-3.2# looks too much like the standard prompt for my tastes--I want something that really reminds me of what I'm doing, to hopefully prevent me from doing something really stupid while operating as root.

If you'd like to change your root prompt, assuming you're using the default bash shell, here's how to do it. Using nano or vi or BBEdit or whatever your favorite pure text editor may be, edit the file named .profile in your user's home directory.

(If this file doesn't exist, create it. There's also a slim chance you may have a file named .bash_profile instead; if you do, edit that file and not .profile.)

To set the root prompt, you need to set a variable named SUDO_PS1 in this file. If you'd like to mimic your normal shell prompt--showing your machine name, current directory, and username, you would add this command to the .profile file:

export SUDO_PS1="\[\h:\w\] \u\# "

Prompt strings are very strange-looking foreign creatures, and so the above probably doesn't make a lot of sense. In a nutshell, \h, \w, and \u display the machine (host) name, the current directory, and the user name. When using sudo -s, the user name will display as root#, versus the usual robg$ when logged in as a normal user.

image

A clearly-differentiated root prompt can prevent stupidity

Save the file, quit the editor, and open a new Terminal window to see your changes take effect (after running sudo -s, of course).

While the above is a good start, it still doesn't stand out enough for me.

So I modified the above to make the differences plainly visible, as you can see in the image at right.

To create my prompt, I used this entry in my .profile file:

export SUDO_PS1="\[\e[33;1;41m\][\u] \w \$\[\e[0m\] "

(Note to true Unix experts: I'm not positive my prompt is perfect; perhaps it could be simplified. However, it works for me, and has done so for many years.) There's a whole lot going on there, and to explain it all in detail would take well more words and space than I have available here. However, the basic breakdown is as follows:

* \[ - what follows are non-printing characters, so they aren't counted for line-wrap purposes

* \e[ - the start of an escape sequence, to set the colors

* 33;1;41m - the actual color string, to create bold yellow text on a red background

* \] - the end of the non-printing characters

* [\u] \w \$ - the user name in brackets, followed by the directory and the # symbol

* \[\e[0m\] - another non-printing sequence; this one sets the colors back to the defaults, so only the prompt is yellow-on-red

You can learn a bit more about prompts by reading the bash manual (type man bash in Terminal). Search (by typing a forward slash, /) for When executing interactively to jump directly to the section on prompting. Even that, though, won't help explain most of the above. For that, you'll need some help from Google. In particular, I like this tutorial for its good overview, and this one for its coverage of color usage.

If this hint inspires you to change your default non-sudo prompt, you can do that, too. Add another line to the .profile file which begins export PS1="..., and then build the prompt string as you wish.

If you mess something up and wind up with an unreadable prompt (What? No, that's never happened to me!), just remove the line from the .profile file, save your changes, and open a new Terminal window. I will caution you, though, that playing with prompt strings can be an amazingly time-consuming diversion..."just one more little change!"


Originally published on Macworld |  Click here to read the original story.

ITworld LIVE

SoftwareWhite Papers & Webcasts

White Paper

Activities Streams Base An Integrated Social Layer

The enterprise social software market is exploding thanks to converging trends of consumerization, cloud, and mobile. In this must-read report, "The Forrester Wave: Activities Streams, Q2 2012", Forrester Research Inc. evaluated five social software vendors with core strengths in the stream based on the overall strength of vendors' current offerings, a clear product strategy, and vendor market presence. In a detailed look at the space, Forrester named Yammer as a leader.

White Paper

ESG Lab Review: HP 3PAR Peer Motion Software

This ESG Lab review sponsored by HP + Intel documents hands-on testing of HP 3PAR Peer Motion Software's distributed volume.Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.

White Paper

ESG Lab Review: HP 3PAR Peer Motion Software

This ESG Lab review documents hands-on testing of HP 3PAR Peer Motion Software's distributed volume management with a focus on federated workload balancing, asset management, and thin provisioning.Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.

White Paper

Deliver Cost-Effective Business Continuity with Extreme Capacity

IBM DB2 provides application cluster transparency technology that equips organizations running OLTP applications with the ability to deliver high availability and continuous uptime for transactional data, plus the flexibility and capacity they need to remain competitive.

White Paper

What Developers Want: The End of Application Redeploys

Eliminate application restarts in Java with JRebel! JRebel is a JVM plugin that eliminates application redeploys from the Java development cycle, a process that takes over 10 minutes of coding time away from developers each working hour, according to a recent survey. Just code, refresh and see everything instantly.

See more White Papers | Webcasts

Ask a question

Ask a Question