Using PS1

suggest change

PS1 is the normal system prompt indicating that bash waits for commands being typed in. It understands some escape sequences and can execute functions or progams. As bash has to position the cursor after the displayes prompt, it needs to know how to calculate the effective length of the prompt string. To indicate non printing sequences of chars within the PS1 variable escaped braces are used: \[ a non printing sequence of chars \]. All being said holds true for all PS* vars.

(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='\[email protected]\h:\w > ' # \u == user, \h == host, \w actual working dir
[email protected]:/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)\]@\w:\w \$ "
[email protected]:/some/path > ▉  # if not root else <red>root<default>@host....

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents