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
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
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
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,
\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.
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
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
\[\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,
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!”