Prompt or shell prompt (the text that informs the user that the system waits for the introduction of its data) can be freely configured. Bash uses 4 different prompts depending on the situation. Their appearance is determined by the contents of variables:
PS1 is visible almost all the time. The primary prompt, default is: PS1="[\u@\h \W]\\$ "
PS2 displayed when the interpreter needs additional data that we have to introduce (eg, forgot to close quote)
PS3 is used as a "prompt" when using the select loop
PS4 is used when using the option-x bash (debug - trace execution mode) - This mode can be obtained by running the Bash with the-x option or by typing "set-o xtrace". There is another way. Before the start of the right keywords to put a shell in the system variable xtrace SHELLOPT
Bash allows you to modify the prompt to a large extent. First, it allows the use of special characters with a backslash viewing sessions and various parameters of the system. Second, variables can contain any of the PS shell variables, arithmetic expressions, or calls to other commands. Some characters, preceded by a backslash are given below:
Special variables:
\D date,
\h host name,
\H full domain name host,
\j number of jobs working in the shell,
\n newline,
\r carriage return,
\s displays the name of the shell
\l terminal device name of the session,
\t hours in 24-hour format,
\T hours in 12-hour format,
\u user name,
\w current working directory,
\W the last part of the current working directory,
\! number of commands in the history of commands
\# number of commands in the current session,
\$ identifying mark of the session, # in case of an administrator, $ in your case.
Several more or less typical examples of changes the prompt is shown below:
bash-2.01$ export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
$ Export PS1 = "[\ u@ \ h \ w]"
johny @ batnet: ~ $ export PS1 = "[\ u@ \ h \ W]"
[Johny @ batnet ~] export PS1 = "(\ t) \ h \ $"
(2:36:32 p.m.) $ batnet
$ Export PS1 = "[\ u@ \ h \ w]"
johny @ batnet: ~ $ export PS1 = "[\ u@ \ h \ W]"
[Johny @ batnet ~] export PS1 = "(\ t) \ h \ $"
(2:36:32 p.m.) $ batnet
The second prompt is displayed in situations such as the omission of the apostrophe or closing quotation marks:
Example
(2:37:14 p.m.) batnet $ export PS2 = ">"
(2:37:14 p.m.) batnet $ echo "Al has
> Cat
> A small and black;) "
(2:37:14 p.m.) batnet $ echo "Al has
> Cat
> A small and black;) "
The third prompt is displayed when you create the menu using the select loop. Example for self-testing.
Example
# / Bin / bash
PS3 = 'Choose your favorite vegetable: '
select vegetable in "beans" "carrots" "potatoes" "onions" "radishes"; to
echo
echo "Your favorite vegetable is $ vegetable."
echo "Ugh!"
echo
break # This break is necessary inaczje infinite loop would be
done
PS3 = 'Choose your favorite vegetable: '
select vegetable in "beans" "carrots" "potatoes" "onions" "radishes"; to
echo
echo "Your favorite vegetable is $ vegetable."
echo "Ugh!"
echo
break # This break is necessary inaczje infinite loop would be
done
Example:
# / Bin / bash
# Go to the mode xtrace
export PS4 = '::'
set-o xtrace
echo "Hello world!"
# Go to the mode xtrace
export PS4 = '::'
set-o xtrace
echo "Hello world!"
Result:
(2:39:37 p.m.) $ sh oo.sh batnet
:: Echo Hello world!
Hello world!
:: Echo Hello world!
Hello world!
In prompt can use the same colors as in bash scripts.