When working in a terminal, the Bash prompt is your gateway to the command-line world. A well-designed prompt can enhance your productivity, provide essential information, and make your shell experience more enjoyable. In this article, we will explore how to set up a useful and visually appealing Bash prompt.
Table of contents
Understanding the Bash Prompt
The Bash prompt is the text that appears in your terminal, indicating that the shell is ready to accept commands. By default, it is typically a simple string, such as $
or #
, followed by a space. However, you can customize it to display valuable information, such as your username, hostname, current directory, and even version control status.
Customizing the Prompt
To customize your Bash prompt, you need to modify the PS1
environment variable. The value of PS1
determines the appearance of your prompt. Let’s explore some useful options for customizing your prompt:
1. Basic Prompt with Current Directory
A straightforward prompt can include your username and the current working directory.
PS1='\u:\W\$ '
Here:
\u
represents the username.\W
displays the basename of the current working directory.
2. Colorful Prompt
Adding colors can make your prompt more visually appealing and easier to read.
PS1='\[\e[1;32m\]\u\[\e[m\] \[\e[1;34m\]\W\[\e[m\]\$ '
Here:
\[\e[1;32m\]
sets the username color to bright green.\[\e[m\]
resets the color to the default.\[\e[1;34m\]
sets the current directory color to bright blue.
3. Git Branch and Status
If you frequently work with Git repositories, you can display the current Git branch and status in your prompt.
PS1='\u:\W$(\_\_git_ps1 " (%s)")\$ '
Before using this prompt, make sure to enable the Git-related prompt functions by adding the following lines to your .bashrc
or .bash_profile
:
source /usr/share/git/git-prompt.sh
4. Multiline Prompt
A multiline prompt can be helpful when dealing with long commands or deep directory structures.
PS1='\[\e[1;32m\]\u\[\e[m\] \[\e[1;34m\]\W\[\e[m\]\n\$ '
Here, \n
inserts a newline, creating a multiline prompt.
5. Showing Exit Status
You can display the exit status of the last command executed.
PS1='\u:\W\$? \$ '
Here, \$?
shows the exit status.
A useful prompt
One of the most useful prompts would include the top three and bottom 2 directories when there are more than 5
So my prompt (which has other info as well) looks like:
when my pwd is actually way bigger.
My PS1
prompt is setup as follows:
HOST='\[\033[02;36m\]\h'; HOST=' '$HOST
TIME='\[\033[01;31m\]\t \[\033[01;32m\]'
LOCATION=' \[\033[01;34m\]`pwd | sed "s#\(/[^/]\{1,\}/[^/]\{1,\}/[^/]\{1,\}/\).*\(/[^/]\{1,\}/[^/]\{1,\}\)/\{0,1\}#\1_\2#g"`'
BRANCH=' \[\033[00;33m\]$(git_branch)\[\033[00m\]\n\$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
git_branch
is a function which shows current git branch, I keep it in my dotfiles, it is:
git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
Making the Changes Permanent
To apply your custom prompt permanently, add the PS1
configuration to your .bashrc
or .bash_profile
file in your home directory.
echo 'PS1="\u:\W\$ "' >> ~/.bashrc
After making the change, reload the shell or run source ~/.bashrc to see the updated prompt.
Conclusion
Customizing your Bash prompt can significantly improve your command-line experience. Whether you prefer a minimalistic design or an information-rich prompt, you now have the tools to create a prompt that suits your needs and style. Experiment with different configurations until you find the one that works best for you. Happy customizing and happy command-lining!