Customizing the Linux Prompt
When working in a Linux terminal, the prompt is your constant companion, guiding you with context about your current environment. However, the default prompt may not provide the exact information or appearance that you prefer. Thankfully, customizing the prompt is not only possible but also a straightforward task.
Saving the Default Prompt
Before we dive into customizations, it's a good practice to save the current state of the prompt. This way, if you ever decide that your customizations are not to your liking, you can easily revert to the default.
To save the default PS1 value, simply execute:
ps1_old=$PS1
With the value stored in ps1_old
, you can later restore the original prompt
using:
PS1=$ps1_old
Basics of Customizing the Prompt
The prompt's appearance and information are dictated by the PS1
environment
variable. By adjusting the value of PS1
, you can customize the look and feel
of your prompt. Remember, the PS1
variable can contain text, variables, and
special escape codes that are replaced with dynamic values.
Certainly! Here are two alternative examples that focus on customizing the information displayed by the prompt without altering its appearance:
Displaying the Last Command's Exit Status
It's useful sometimes to know the exit status of the last command you ran, especially when scripting. A zero (0) exit status usually indicates success, and a non-zero status indicates an error. You can have this information right in your prompt:
PS1='\u@\h:\w (last cmd: $?)\$ '
After running a command, the prompt will show the exit status of that command in
parentheses. For instance, after a successful ls
command, it might
show (last cmd: 0)
.
Displaying Only the Current Directory
If you find the full path too verbose, you can display just the current directory name:
PS1='\W\$ '
Showing the Time
You can include the current time in your prompt, which can be particularly useful if you're tracking the time of your commands:
PS1='\t \u@\h:\w\$ '
Adding Emoji or Special Characters
To make your prompt more playful, you can include emojis or special characters:
PS1='🚀 \u@\h:\w\$ '
Advanced Customizations
Display Host IP Address
If you're often working with networking or switching between different networks, having the host's IP address in the prompt can be handy:
PS1='\u@\h (\4{eth0}):\w\$ '
This would show the IPv4 address of the eth0
network interface. For example,
if the IP address is 192.168.1.10
, the prompt might appear
as username@hostname (192.168.1.10):~$
.
Note: The above command might not work on all systems or setups since the way to
retrieve the IP address might differ. Ensure your interface name matches (eth0
in this case), or adjust as needed. If your system doesn't support \4
, you
might have to use a command substitution with a tool like ip
or ifconfig
to
achieve similar results.
Calling Commands
You can incorporate the result of a command into your prompt. As an example, you can display the number of files in the current directory:
PS1='\u@\h:\w ($(ls | wc -l) files)\$ '
Restoring the Original Prompt
After experimenting with various customizations, if you want to go back to the
default Ubuntu prompt, simply recall the ps1_old
value you saved earlier:
PS1=$ps1_old
Conclusion
The prompt is an essential aspect of the Linux terminal experience, and customizing it can make your interactions more efficient, informative, and enjoyable. Whether you're changing colors, adding emojis, or incorporating dynamic data, the prompt offers a canvas for your creativity. Always remember to save your original prompt value, ensuring you have a way back to familiar ground if needed.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.