Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

CustomBashPrompt

From MTU LUG Wiki
1. default prompt "\u@\h:\w\$ "
        \u      username
        \h      hostname
        \w      current working directory
        \$      # if UID is 0 (root), $ otherwise
2. Pretty colors! \[ and \] keep bash from using the enclosed text in prompt-length calculations
        \e[XX;YYm for color, XX is foreground, YY is background. ;1 gives bold foreground, background unnecessary 
                if default desired
        \e[0m resets to default, useful at end of prompt.

        Foreground
        30 black
        31 red
        32 green
        33 yellow
        34 blue
        35 purple
        36 teal
        37 white

        Background
        40 black
        41 red
        42 green
        43 yellow
        44 blue
        45 purple
        46 teal
        47 white
        "\[\e[32;1m\]\u@\h:\w\$ \[\e[0m\]" all green
        "\[\e[31;1m\]\u\[\e[0m\]@\[\e[33;1m\]\h\[\e[0m\]:\[\e[36;1m\]\w\[\e[32;1m\]\$ \[\e[0m\]" red user, yellow 
                hostname, teal cwd, green UID symbol
3. Informative but kind of bulky and boring. In .bashrc, we can test if we are in an xterm or not using:
        if [ "$TERM" = "linux" ]
If we are, let us put some information up in the xterm titlebar, using the escape sequences "\[\e]2;" "\a"
        if [ "$TERM" = "linux" ]
        then
                export PS1="\[\e[32;1m\]\u@\h:\W\$ \[\e[0m\]"
        else
                export PS1="\[\e]2;\u@\h:\W\a\e[32;1m\]\$\[\e[0m\] "
        fi
Puts username, host, and base of cwd in titlebar, and leaves the UID symbol as prompt in green
4. Scripts! So, if we have scripts that produce output without newlines, we can include them in our prompt by
                enclosing the call in $()
        Battery status is useful on laptops, so we have a script that puts percentage and status (charging, 
                discharging, or charged) in a compact form, and colors the percentage based on number (red under 
                15%, yellow under 30%, green else
        "[ \$(/home/agmlego/bin/battery-status) ] \[\e]2;\u@\h:\W\a\e[32;1m\]\$\[\e[0m\] " Battery status followed 
                by UID symbol in green, username/host/cwd is in titlebar
5. Further customization:
        \a      an ASCII bell character (07)
        \d      the date in "Weekday Month Date" format (e.g., "Tue May 26")
        \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
        \u      the username of the current user
        \v      the version of bash (e.g., 2.00)
        \V      the release of bash, version + patchlevel (e.g., 2.00.0)
        \w      the current working directory
        \W      the basename of the current working directory
        \!      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