I was working on my bash prompt recently. I think I’ve settled on some defaults that I quite like. I figured I’d share it with the world. For the TL;DR it’s:
export PS1='\n\e[01;32m\w\e[0m\n: \u@\h ; ' export PS2='\e[4m\011\e[0m '
Your top-level, main prompt is PS1. That’s the one you see most of the time. Historically (as in 40 years ago) it was just “$” if you were not root, and it was “#” if you were root. PS2 is your second level prompt. If you’re like me, you type shell scripts and long commands at the command line. In that case, PS2 is what you see at the beginning of each secondary line. Take a look at Figure 1 to see the two prompts. These are also the default values for bash if you don’t change them.
If you’re looking for an easy way to make your own prompt, check out the Easy Bash Prompt Generator online. I’m going to explain why I have my prompts set the way I do.
For my main prompts I use the values:
export PS1='\n\e[01;32m\w\e[0m\n: \u@\h ; '
That particular one uses ansi color 32 for the current working directory. If I’m on my laptop, that’s named ‘Lynx’ and I’m in my ~/Documents directory, then the prompt looks like:
- paco@Lynx ;
The current working directory is yellow, there’s a newline, and then there’s the prompt. One thing I tend to do is use different colours on the directory. All the various values are documented online. On my personal laptop i use yellow. On some VMs I use green or red depending on what system it is. It’s just one more visual hint about where I am and what I’m doing. For a complete document about all the different colours you can use, check this StackOverflow answer.
Why do I use colon and semi-colon on my prompt? I’m glad you asked.
Colon and Semi-Colon
The colon/semi-colon trick is something an old friend taught me back around 1998. You might not know, but colon is a shell command. If I type a command like:
: this is a simple example ; echo "foo"
Then I will simply see “foo” as output. The colon command is the no-op. It does nothing. It accepts all manner of arguments. The semi-colon delimits commands. So this is basically “no-op” followed by an actual command (“echo foo”).
This means I can triple-click to select a command line I previously typed, and simply ⌘-C ⌘-V to copy and paste it. The prompt is ignored and the command executes. Take a look at the video in Figure 2.
See how when I triple-click it selects the entire line, including my prompt. But my prompt is inert. It doesn’t execute anything. And the command itself does execute. I find this to be a really handy shortcut. If you use more traditional prompts with symbols like
$ in them you can’t do this. Common Linux prompts like
PS1=”[\d \t \u@\h:\w ] $ “ will produce prompts like:
[Sat Mar 18 12:41:03 paco@Lynx:~/Documents/blog ] $
This might be attractive to look at, but if I copy/paste it, I’ll see an error like
-bash: [Sat: command not found.
In my opinion, if you want a ton of information in every prompt, do what I do and put a new-line in there. You could have a prompt like this and still have the copy/paste capability:
PS1="[\d \t \w ] \n: \u@\h ; "
That produces a prompt like this:
[Sat Mar 18 12:50:34 ~/Documents/blog ] : paco@Lynx ;
So what about long command lines? PS2 is again quite handy. I need some visual indication that I’m in PS2. At the same time, I want to be able to copy/paste lines from any scripts I type at the command line. I set my PS2 to be:
export PS2='\e[4m\011\e[0m '
I’m taking advantage of the fact that tab characters can be underlined. The \e[4m turns on underlining. \011 is the TAB (more correctly the horizontal tab) character. Then the \e[0m turns off underlining. The result is something that looks like what you see in Figure 3.
The _____ that you see at the command line is my PS2 prompt. But if I triple-click and copy the line with
echo "google: $host" it will paste and execute correctly. The tab characters are just inert white space ignored by the shell.
There you go: a few nice prompts for bash that are especially copy/paste friendly. The visual distinction by colour and the inert white-space for a second-level prompt are pretty handy in this day and age where all our terminals can do colour and highlighting.