Using PS1
suggest changePS1 is the normal system prompt, indicating that bash is waiting for commands to be typed. It understands some escape sequences and can execute functions or programs. Because bash has to position the cursor after the displayed prompt, it needs to know how to calculate the effective length of the prompt string. To indicate non-printing sequences of characters within PS1, escaped braces are used: \[ a non-printing sequence of characters \]. All of this holds true for all PS* variables.
(The black caret indicates cursor)
#everything not being an escape sequence will be literally printed
export PS1="literal sequence " # Prompt is now:
literal sequence ▉
# \u == user \h == host \w == actual working directory
# mind the single quotes avoiding interpretation by shell
export PS1='\u@\h:\w > ' # \u == user, \h == host, \w actual working dir
looser@host:/some/path > ▉
# executing some commands within PS1
# following line will set foreground color to red, if user==root,
# else it resets attributes to default
# $( (($EUID == 0)) && tput setaf 1)
# later we do reset attributes to default with
# $( tput sgr0 )
# assuming being root:
PS1="\[$( (($EUID == 0)) && tput setaf 1 )\]\u\[$(tput sgr0)\]@\h:\w \$ "
looser@host:/some/path > ▉ # if not root else <red>root<default>@host....
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents