Anotações / Linux
Customizar Prompt de comando com variaveis de ambiente
There are several variables that can be set to control the appearance of the bash command prompt: PS1, PS2, PS3, PS4 and PROMPT_COMMAND the contents are executed just as if they had been typed on the command line.
PS1 – Default interactive prompt (this is the variable most often
customized)
PS2 – Continuation interactive prompt (when a long command is broken up
with at the end of the line) default=“>”
PS3 – Prompt used by “select” loop inside a shell script
PS4 – Prompt used when a shell script is executed in debug mode (“set -x” will turn this on)
default =“++”
PROMPT_COMMAND - If this variable is set and has a non-null value, then
it will be executed just before the PS1 variable.
Set your prompt by changing the value of the PS1 environment variable, as follows:
$ export PS1=‘My simple prompt$’
My simple prompt$
It is recommended not to use a ‘>’ in your PS1 Command Prompt as if copy/pasted by accident that can cause a redirection and file overwrite.
This change can be made permanent by placing the export
PS1 definition in your ~/.bashrc file.
To see what is currently defined in your .bashrc/.bash_profile use grep
‘PS1’ $HOME/.bash{rc,_profile}
The default value on many distros is ‘-’
Special prompt variable characters:
he date, in “Weekday Month Date” format (e.g., “Tue May 26”).
The hostname**, up to the first .
(e.g. deckard)
he hostname. (e.g. deckard.SS64.com)
The number of jobs currently managed by the shell.
The basename of the shell’s terminal device name.
The name of the shell, the basename of
$0 (the portion following the final slash).
he time, in 24-hour HH:MM:SS format.
The time, in 12-hour HH:MM:SS format.
@ The time, in 12-hour am/pm format.
The username** of the current user.
he version of Bash (e.g., 2.00)
The release of Bash, version + patchlevel (e.g., 2.00.0)
* The current working directory**.
The basename of
$PWD.
! The history number of this command.
# The command number of this command.
$ If you are not root, inserts a “$”; if you are root, you get a “#” (root uid = 0)
_nnn_ The character whose ASCII code is the octal value nnn.
\n A newline.
\r A carriage return.
\e An escape character (typically a color code).
\a A bell character.
\ A backslash.
[ Begin a sequence of non-printing characters. (like color escape
sequences). This
allows bash to calculate word wrapping correctly.
] End a sequence of non-printing characters.
Using single quotes instead of double quotes when exporting your PS variables is recommended, it makes the prompt a tiny bit faster to evaluate.
Command/History Numbers
The command number and the history number are usually different: the history number of a command is its position in the history list, which can include commands restored from the history file, while the command number is the position in the sequence of commands executed.
After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the
promptvarsshell option.
Color Codes (ANSI Escape Sequences)
Foreground colors, Normal (non-bold) is the default, so the 0; prefix is optional.
(https://ss64.com/bash/tput.html), this will read the terminfo database with all the escape codes necessary for interacting with your terminal.
If you use tput instead of hard-coding the escape sequences, you can set the TERM variable to control whether color codes are output. This also makes the output portable to terminals other than xterm.
Examples
Set a prompt like: hostname:~/CurrentWorkingDirectory$
export PS1=‘$’
Set a prompt like: [username@hostname:~/CurrentWorkingDirectory]$
export PS1=‘[**@*]$’
Set a prompt
like [username@hostname:~/CurrentWorkingDirectory]$
With the foreground color red, note the escapes for the non printing
characters [ ] become [ and ], these ensure that readline can keep track
of the cursor position correctly. The 0m at the end resets the color
back to default.
export PS1=’[[31m]*[[0m]** ’
Set a prompt like [username@hostname:~/CurrentWorkingDirectory]$ with the foreground and background colors, set via tput:
magenta=$(tput setaf 5)
blue=$(tput setaf 4)
reset=$(tput sgr0)
export PS1=’**[[$magenta][$reset]**@**[$magenta]:[$blue]***[$reset]**]**$ ’
Use the $PIPESTATUS array to capture the exit status of every piped command and set a prompt like username@hostname $
export PROMPT_COMMAND=”
_RES=${PIPESTATUS[*]};
_RES_STR=’’;
for res in $_RES; do
if ( \$res > 0 ) ; then
_RES_STR=" [$_RES]";
fi;
done”export PS1=“$_RES_STR\$ ”
“There is a time for departure even when there’s no certain place to go” ~ Tennessee Williams
Related Linux commands
env - Display, set, or
remove environment variables.
export - Set an environment variable.
Bash $PS1
generator - Generate a custom colorful bash prompt. (tput
codes).
Bash
Prompt HowTo
Windows equivalent: PROMPT - Change the command
prompt.