🌐 🇵🇱 Polski · 🇬🇧 EN
If you have ever worked with Linux, you certainly know what a terminal window is. In the terminal, we also have something called a command prompt. For those who don't know, this prompt is the first line where we type our commands. It can take various default forms depending on the distribution, but it is most commonly something like [root@hostname ~] or, for example, -bash4.2#. However, you can easily customize it to display other information for convenience. The prompt can also look like this: [12:45] [root@hostname] [/location] $, where the time, logged-in user, hostname, and current working directory are visible. The prompt can also be colorized. Colors make it more readable and help it stand out from the rest of the command output. The system variable that stores the prompt format is called "PS1"; to see its current format, simply run the command echo $PS1.
Step 1 - Setting the prompt to [time] [user@server] [location]
In the terminal, run the following command:
export PS1='[\A][\u@\h][\w] $ '
After executing the above, the prompt will look like this:
[04:13][root@an-server][/opt]
Step 2 - Colorizing the prompt.
In the terminal, run the command that includes color codes so that the time is yellow, the username is green, the hostname is red, and the location is cyan.
export PS1='[\[\e[0;33m\]\A\[\e[m\]][\[\e[0;32m\]\u\[\e[m\]@\[\e[0;31m\]\h\[\e[m\]][\[\e[0;36m\]\w\[\e[m\]] $ '
Although the above may look quite complex, in reality, everything forms a logical structure.
If you want to apply coloring in the terminal, the standard pattern used is:
\e[X;Ym TEXT_TO_COLOR \e[m
- \e[ - Starts the coloring
- X;Y - A pair of colors (listed in the table below)
- m - Mandatory character
- \e[m - Character sequence ending the coloring
To find the values for the X;Y pair, you can use the colors.sh script:
esc="\033["
echo -n " _ _ _ _ _40 _ _ _ 41_ _ _ _42 _ _ _ 43"
echo "_ _ _ 44_ _ _ _45 _ _ _ 46_ _ _ _47 _"
for fore in 30 31 32 33 34 35 36 37; do
line1="$fore "
line2=" "
for back in 40 41 42 43 44 45 46 47; do
line1="${line1}${esc}${back};${fore}m Normal ${esc}0m"
line2="${line2}${esc}${back};${fore};1m Bold ${esc}0m"
done
echo -e "$line1\n$line2"
done
The output will be:
Step 3 - Saving settings for the user in .bash_profile
If you want the prompt to look as we designed above every time you log into the terminal, the PS1 variable setting must be saved in the .bash_profile file
#Prompt
export PS1='[\[\e[0;33m\]\A\[\e[m\]][\[\e[0;32m\]\u\[\e[m\]@\[\e[0;31m\]\h\[\e[m\]][\[\e[0;36m\]\w\[\e[m\]] $ '
The final result for the prompt looks like this:
Useful links:



Comments