A detailed look into how, and why, to customize a bash prompt.
Hosted by Windigo on 2017-08-03 is flagged as Explicit and is released under a CC-BY-SA license.
Tags: bash,command line,configuration,terminal.
Listen in ogg,
mp3 format. | Comments (3)
This is an open series in which Hacker Public Radio Listeners can share their Bash scripting knowledge and experience with the community. General programming topics and Bash commands are explored along with some tutorials for the complete novice.
Basic bash prompt information
- Variables and files
.bashrc: the RC file where all of this stuff can be set
PS1: main prompt variable
PS2: continuation prompt
PROMPT_COMMAND: a bash function name, run every time prompt is displayed
- Uses escape sequences
- There are problems with prompts and escape sequences
- Multiple escaping
- String interpretation and variable expansion
- tput to the rescue!
- Takes away the need for complex escape codes
- Must run
tput init at the beginning of your
- TL;DR: Use tput for color strings, add them at the last possible moment, with brackets and backslashes
- Embedding bash scripts
- Single quotes are king
- Will be run every time PS1 is evaluated
- Otherwise, only run at time of assignment
Layout of my prompt
- Two lines
- Information/status line
- Prompt line
- Status line
- Starts with current username
- Changes color when user has mail
- Next is the hostname (truncated)
- Separated by an @ symbol, like an email address
- Changes color when the system is in need of a reboot
- Checks for /run/reboot_required
- Current directory
- Separated from previous items by a pipe
- Truncated with a tilde if user's home is in the path
- Prepended with a number indicating the directory stack, if present
- Appended with the git (±) symbol if we're in a git branch, followed by the name of the branch
- Prompt line
- A blue » character
- Prepended with the number of background processes spawned from this terminal
Link to Git Repository
Editor's Note: added 2017-08-05
Comment #1 posted on 2017-08-04T12:15:27Z by x1101
prompt for other users
Loved the episode. Very well articulated! I had one thought. When you want to run your prompt as another user (or, have other-user specific prompts), you don't need to do any symlinking, especially on a multi-user system.
For example, on servers I helped manage, I had .rootbashrc in my home directory, and after I did a sudo su - to get a root shell, I would then source /home/x1101/.rootbashrc to get _my_ root specific rc file.
Just some food for thought.
Comment #2 posted on 2017-08-05T03:15:45Z by Windigo
Quick follow up: Xoke was kind enough to remind me that I hadn't posted a link anywhere to my configurations. Here's the git repository:
x1101: That makes a lot more sense, especially on multi-user systems, since you wouldn't want to steamroll other users' rc files with your own. Thanks for the tip, man! :)
Comment #3 posted on 2017-08-12T16:43:00Z by Dave Morriss
Great show - most enjoyable
Thanks for doing this show. I enjoyed it a lot (even though I'm rather late listening to it).
I have done stuff to my prompts in the past, on Unix systems and on Linux, but have just not bothered in later years. I like the ideas you talk about here and may well be inspired to experiment some more.
<< First, < Previous, Next >, Latest >>
Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.
Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).