Skip to main content

Bash Profiles

Organizing Bash Settings

The following block contains a list of files related to bash, and their location / use.

           /bin/bash
                  The bash executable
           /etc/bash.bashrc
                  The system-wide bashrc for interactive bash shells, invoked on any login to an interactive shell.
           /etc/skel/.bashrc
                  Used as a template for new users when initializing a basic .bashrc in their home directory. 
           /etc/profile
                  The systemwide initialization file, executed for login shells
           /etc/bash.bash_logout
                  The systemwide login shell cleanup file, executed when a login shell exits
           ~/.bash_profile
                  The personal initialization file, executed for login shells
           ~/.bashrc
                  The individual per-interactive-shell startup file
			~/.bash_aliases
                  An optional file sourced by .bashrc by default
           ~/.bash_logout
                  The individual login shell cleanup file, executed when a login shell exits
           ~/.inputrc
                  Individual readline initialization file

I often refer to the bash reference found within the bash-doc package. For Ubuntu, run sudo apt install bash-doc and navigate to the /usr/share/doc/bash-doc/ directory for a collection of examples and more detailed bash references. To help you explore these files, consider installing the terminal file browser ranger with sudo apt install ranger.

Creating User Profiles

Create a custom bash profile for your user within their home directory by modifying ~/.bashrc. The important lines to customize are seen below -

# Bash prompt settings

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

# Alias / export customizations

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi1

\[\033[1;31m\]\h\336\220\u\336\220\[\033[0;32m\]\w\[\033[1;31m\]]\@:\340\264\275_\$?\n\337\206\s\[\033[0;32m\]\342\225\274$

# Auto-completion

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

Identifying Unicode Symbols for use in .bashrc

Character search engine

If you dont have access to a terminal, you can search up a symbol to get UTF8, see the below character and the corresponding UTF8 format as an example. ഽ = 0xE0 0xB4 0xBD

Hexdump unicode symbol

Most linux systems already have hexdump installed, so we could also run echo ސ | hexdump -C to see the following output -

[kapper@kubuntu-vbox ~]$echo ސ | hexdump -C
00000000  de 90 0a
00000003

From this output, we can see that the UTF8 hexidecimal format of our symbol is \xde\x90\x0a. Using this information, we can test the coloring the character green with the echo statement below.

echo -e '\001\033[1;32m\xde\x90\x0a\033[0m\002'

Unicode.vim

Worth mentioning that if you are using vim, an easy to use plugin that is useful for identifying characters is unicode.vim. See my notes on Unicode vim plugin for more information, or check out the official Unicode vim repository.

Bash Aliases

create a list of aliases within your home directory within a file named .bash_aliases, or feel free to customize the following lines in your local .bashrc

# ~/.bashrc
# Alias / export customizations

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

For example, I decided to move most of the default information above to my own .bash_aliases file, so I could edit it all in one place. I make my edits at the top of the file with additional custom aliases.

alias gitkapp='git config --global user.name "Shaun Reed" && git config --global user.email "shaunrd0@gmail.com"'


# Default .bashrc aliases stored here

# Alias / export customizations

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

The gitkapp alias above is a quick way of telling git who I am when logged in as a new user. Aliases even show up using auto completion -

[user@host ~]$git
git                 git-shell           git-upload-pack     
git-receive-pack    git-upload-archive  gitkapp             
[user@host ~]$gitkapp 

Environment Variables

PS1: environment variable which contains the value of the default prompt. It changes the shell command prompt appearance and environment.

PS2: environment variable which contains the value the prompt used for a command continuation interpretation. You see it when you write a long command in many lines.

PS3: environment variable which contains the value of the prompt for the select operator inside the shell script.

PS4: environment variable which contains the value of the prompt used to show script lines during the execution of a bash script in debug mode.

Bash Prompt

Your bash prompt is whats seen before you type a command -

user@host:~/$ 

This is defined by the PS1 variable within your .bashrc -

# Bash prompt settings

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

There's two settins to configure, the first block includes color, the second does not.

You can change this using any variety of the settings below. After you've got a good export working, paste it into the ~/.bashrc to apply your changes each time you login. If you skip this step and log out of your terminal with an export applied, when you login it will be overwritten by the code above.

Export Examples

Any of the below exports can be pasted directly into the terminal to be tested. Once the terminal is closed, these settings will be lost, so no worries about getting back to default. This is a good way to test what would happen if you changed the PS1 within your .bashrc, without actually doing so.

Below are some examples, each more complicated than the last to show how many options are available. These, of course are still only a few.

export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\][\u@\h\[\033[00m\] \W\[\033[01;32m\]]\$\[\033[00m\]'
export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\][\u]\[\033[00m\] \w\[\033[00m\]\n\[\033[01;32m\]\337\206\[\033[00m\]\[\033[10;92m\]\h\[\033[00m\]\[\033[01;32m\]]\W:\$\[\033[00m\] '

No color Parrotsec

export PS1='┌──˨\u@\h─[\w]\n└──╼\$`

Colorized Parrotsec

export PS1="\[\033[0;31m\]\342\224\214\342\224\200\$([[ \$? != 0 ]] && echo \"[\[\033[0;31m\]\342\234\227\[\033[0;37m\]]\342\224\200\")[$(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]root\[\033[01;33m\]@\[\033[01;96m\]\h'; else echo '\[\033[0;39m\]\u\[\033[01;33m\]@\[\033[01;96m\]\h'; fi)\[\033[0;31m\]]\342\224\200[\[\033[0;32m\]\w\[\033[0;31m\]]\n\[\033[0;31m\]\342\224\224\342\224\200\342\224\200\342\225\274 \[\033[0m\]\[\e[01;33m\]\\$\[\e[0m\]"
Bash Options
$PS1 : Your shell prompt variable
\e[ : Start color scheme.
\033: equivalent to \e as an escape code
x;y : Color pair to use (x;y)
\e[m : Stop color scheme.
\H: Display FQDN hostname.
\@: Display current time in 12-hour am/pm format
\u: Display the current username .
\h: Display the hostname
\w: Print the entire working directory.
\W: Print the base of current working directory.
\$: Display # (indicates root user) if the effective UID is 0, otherwise display a $.

So we can create a simple export like the below, to test these values before adding any color

export PS1='${debian_chroot:+($debian_chroot)}FQDN: \H | Host: \h \w\n\u\W:\$'

More Prompt References

\a The ASCII bell character (you can also type \007)
\d Date in “Sat Sep 04″ format
\e ASCII escape character (you can also type \033)
\h First part of hostname (such as “mybox”)
\H Full hostname (such as “mybox.mydomain.com”)
\j The number of processes you’ve suspended in this shell by hitting ^Z
\l The name of the shell’s terminal device (such as “ttyp4″)
\n Newline
\r Carriage return
\s The name of the shell executable (such as “bash”)
\t Time in 24-hour format (such as “23:59:59″)
\T Time in 12-hour format (such as “11:59:59″)
\@ Time in 12-hour format with am/pm
\u Your username
\v Version of bash (such as 2.04)
\V Bash version, including patchlevel
\w Current working directory (such as “/home/koithara”)
\W The “basename” of the current working directory (such as “koithara”)
\! Current command’s position in the history buffer
\# Command number (this will count up at each prompt, as long as you type something)
\$ If you are not root, inserts a “$”; if you are root, you get a “#”
\xxx Inserts an ASCII character based on three-digit number xxx (replace unused digits with zeros, such as “\007″)
\\ A backslash

\[ This sequence should appear before a sequence of characters that don’t move the cursor (like color escape sequences). This allows bash to calculate word wrapping correctly.
\] Same as \002, This sequence should appear after a sequence of non-printing characters.

\001 can be used directly in place of \[ and is recommended as a more portable option
\002 can be used directly in place of \] and is recommended as a more portable option

To take a closer look, lets break down the default Parrotsec PS1 value, which contains unique colorization, formatting and symbols. Starting with the first and worst example below, we threw some color codes, bash variables (\h,\w, etc), and copied some symbols from google into the below. This is a misuse of escape sequences [ and \], and uses symbols directly in our .bashrc, which isn't allowed.

'\e[\[0;31m\]\hސ\uސ[\e[\[0;32m\]\w\e[\[0;31m\]]\@:ഽ_\$?\n߆\s\e[\[0;32m\]╼$'

Correct escape sequences, misuse of symbols -

'\[\033[1;31m\]\hސ\uސ f\[\033[0;32m\]\w\[\033[1;31m\]]\@:ഽ_\$?\n߆\s\[\033[0;32m\]╼$'

Correct escape sequences and symbols:

'\[\033[1;31m\]\h\336\220\u\336\220\[\033[0;32m\]\w\[\033[1;31m\]]\@:\340\264\275_\$?\n\337\206\s\[\033[0;32m\]\342\225\274$

On some systems, its required to use \001 in place of \[ and \002 in place of \]. Since this method also works on systems that support \[ and \], so this could be a more portable option.

'\001\033[1;31m\002\h\336\220\u\336\220\001\033[0;32m\002\w\001\033[1;31m\002]\@:\340\264\275_\$?\n\337\206\s\001\033[0;32m\002\342\225\274$'

Note that we do not close an escape sequence until we are finished. See the below for examples

# Ok
echo -e '\001\033[1;32m\xde\x90\x0a\033[0m\002'
# Wrong, no need to close `\001` with `\002` until we are finished with the entire sequence
echo -e '\001\033[1;32m\002\001\xde\x90\x0a\002\001\033[0m\002'
Color codes

Using the appropriate bash syntax and following the codes below,
Toss this escape code in to colorize everything green after:
\001[0;32m\002

Black 0;30 Dark Gray 1;30
Blue 0;34 Light Blue 1;34
Green 0;32 Light Green 1;32
Cyan 0;36 Light Cyan 1;36
Red 0;31 Light Red 1;31
Purple 0;35 Light Purple 1;35
Brown 0;33 Yellow 1;33
Light Gray 0;37 White 1;37

System Profile

When a bash profile is not placed in the home directory, the default profile is loaded from /etc/profile. This file simply checks for a local configuration in the user home directory, and if it does not exist it applies default settings. If you would like to edit or specify certain settings for certain users, a .bashrc file can be created or reconfigured, depending on your current setup. The page below will show some basic syntax for editing the file, along with some examples. The default bash profile can be seen in the code block below.

In short, to set a system-wide variable, such as EDITOR='/usr/bin/vim', simply add the following line to /etc/profile -

    # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
    # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

    # This line sets the system-wide default text editor to vim
    export EDITOR='/usr/bin/vim'
    export VISUAL='/usr/bin/vim'

    if [ "${PS1-}" ]; then
      if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
        # The file bash.bashrc already sets the default PS1.
        # PS1='\h:\w\$ '
        if [ -f /etc/bash.bashrc ]; then
          . /etc/bash.bashrc
        fi
      else
        if [ "`id -u`" -eq 0 ]; then
          PS1='# '
          # This block allows for configuring any user whos id == 0
          # In other words, these settings will be applied to the root user only.
        else
          PS1='$ '
          # These settings will apply in all other cases, system-wide
          # In other words, upon successful login to an authroized user, this block will be executed
        fi
      fi
    fi

    if [ -d /etc/profile.d ]; then
      for i in /etc/profile.d/*.sh; do
        if [ -r $i ]; then
          . $i
        fi
      done
      unset i
    fi

The above /etc/profile configuration will set the default editor to vim, system-wide, regardless of which user is logged in. This includes the root user.

If you want to specify which user, or if you want to handle the root user independent from the rest of the system, take a closer look at the comments I've added in the above configuration file and modify as needed.

If you are trying to change the default text editor for any command ran with sudo, be sure that you pass the -E or --preserve-env argument to sudo when attempting to test these settings. So, if we wanted to preserve our environment setting for the default text editor when running vigr or visudo we would simply run sudo -E vigr or sudo --preserve-env visudo to ensure these settings are referred to when using sudo