Quantcast
Channel: Linux on the mainframe
Viewing all articles
Browse latest Browse all 18

The bash prompt – modify it for your needs

$
0
0

If you're opening a shell it is appearing of course: the bash prompt. There a lot of shares in the web already. I'm talking only about the general login prompt this time, not of the .bashrc which allows to customize some settings for a specific user.

As I'm working with a lot of linux guests it's difficult to keep the overview as admin, especially if you've to copy files between them for example. And in the lab environment the linux guests are changing often depending the projects are running. So it is helpful for me if I'm changing the prompt to get an information - for example the last part of the ip address or if you like the whole address.Currently I'm working on a SLES 11 SP2 linux guest named LINUX157 using the ipaddress 10.199.10.106 on a system z 10 BC.

To see how the prompt is defined just echo its variable $PS1:

LINUX157:/etc # echo $PS1
\[\]\h:\w # \[\]

This prompt shows me the hostname \h and the current working directory \w, divided by a colon (:). But there a lot of possibilities to change the prompt including the color. Our next best friend ist the man bash, which describes in the section "PROMPT" yet another options we could insert.

PROMPTING
When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:  
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format} 

the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
\e an ASCII escape character (033)
\h the hostname up to the first `.'
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell's terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion
following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patch level (e.g., 2.00.0)
\w the current working directory, with $HOME abbreviated with
a tilde
\W the basename of the current working directory, with $HOME
abbreviated with a tilde
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters, which could
be used to embed a terminal control sequence into the
prompt
\] end a sequence of non-printing characters

First we could try out some prompt "layouts" on the bash directly, means also temporary. I only would like to see the Hostname and the time, folling by a '>'. Just type in the following, press enter and you'll have the new prompt.

LINUX157:/etc # PS1="\h-\A> "
LINUX157-13:09>

If you want get back the original general shell login prompt, change into the directory /etc.

In SLES reactivate the file bash.bashrc typing 'source bash.bashrc'. The short alternative is to type just a dot instead of source '. bash.bashrc'.Et voilà - we're back.

LINUX157-13:09> source bash.bashrc
LINUX157:/etc #

 

Within the file bash.bashrc you could find the general defined shell login prompt around row 190.

# With full path on prompt
PS1="${_t}${_u}:\w${_p} "
# # With short path on prompt
# PS1="${_t}${_u}:\$(spwd)${_p} "
# # With physical path even if reached over sym link
# PS1="${_t}${_u}:\$(pwd -P)${_p} "

To customize your general login prompt it's recommended to add an entry in the file /etc/bash.bashrc.local where you also could insert the global aliases you like. After a reboot these settings are globally permanent. Make your entry and activate it using '. bash.bashrc.local'. If you d'like get back the original prompt, set a hashtag in front of your customized $PS1 entry within bash.bashrc.local and source the bash.bashrc again '. bash.bashrc".

 

 In RHEL you may change the file /etc/bashrc first. Remove the three hashtags (#) around row 38. Now you're able to switch back to your original prompt every time using the command '. /etc/bashrc'.

# You might want to have e.g. tty in prompt (e.g. more virtual machines) 

# and console windows
# If you want to do so, just add e.g.
if [ "$PS1" ]; then
PS1="[\u@\h:\l \W]\\$ "
fi
# to your custom modification shell script in /etc/profile.d/ directory

To customize your general login prompt it's recommended to add an entry in the file /etc/profile.d/custom.sh where you also could insert the global aliases you like. As I mentioned at the beginning I'd like to add the last part of the IP address and some colors as an example:

PS1="[\[\e[31;1m\]\u@\[\e[32;1m\]\H\[\e[34;1m\]>106:\[\e[33;1m\]\w\[\e[0m\]] "
[root@LINUX157>106:/etc]

 

PS1="[ \[\e[31;1m\] \u@ \[\e[32;1m\] \H \[\e[34;1m\] >106: \[\e[33;1m\] \w \[\e[0m\] ] "

[                             Starts with a bracket
\[\e[31;1m\]          color def: Red Bold
\u@                       username + @
\[\e[32;1m\]          color def: Green Bold
>106:                     last part of the ip address
\[\e[33;1m\]          color def: Yellow Bold
\w                          path
\[\e[0m\]               switch off color definitions
]                             Ends with a bracket

Please, see the definitions of the colors in the following figure. The appropriate script you'll find at the end of this post:

To check and see the colors in your specific terminal you may use a script. That gives you an overview. Of course the view is depending of your terminal settings you defined. I'm usually working with a black background and green text. Typically mainframe, right?

And here the code regarding the color script 'shellcol'. You could download it HERE. Please rename the file and modify it with chmod +x shellcol.sh.

#! /bin/bash
# Script:'shellcol' from Daniel Schwarzentruber, created on Wed, 13. Mar 2013

Colrange1=( Black/Gray Red Green Yellow Blue Magenta Cyan Gray/White )
Optval=( 1 4 5 7 )
Numrange1=30

echo " "
echo "---------------"
echo "Standard colors"
echo "---------------"
for (( i=0; i < ${#Colrange1[@]}; i++ ))
do
echo -e "0;$Numrange1 = ""\e[0;"$Numrange1"m "${Colrange1[i]}"\e[0m"
(( Numrange1++ ))
done

echo "-------------------------------------------------------"
echo " Options "
echo " (1) Bold; (4) Underlined; (5) Blinking; (7) Reverse "
echo "-------------------------------------------------------"

Numrange1=30
for (( i=0; i < ${#Optval[@]}; i++ ))
do
echo -e ""${Optval[i]}";"31 = "\e["${Optval[i]}";31m "Text sample..."\e[0m"
(( Numrange1++ ))
done
echo " "
echo " "

exit 0

The post The bash prompt – modify it for your needs appeared first on Linux on the mainframe.


Viewing all articles
Browse latest Browse all 18

Trending Articles