Bash Profiles
Creating Profiles
The following block containsCreate a listingcustom ofprofile bashfor profiles,your locations,user andwithin theiryour use.
home directory by modifying. The
/bin/bash.bashrcbashimportantexecutable /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_logout The individual login shell cleanup file, executed when a login shell exits ~/.inputrc Individual readline initialization file
In short,lines to setcustomize aare system-wideseen variable, such as EDITOR='/usr/bin/vim', simply add the following line to
below -/etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
# ThisBash lineprompt sets the system-wide default text editor to vim
export EDITOR='/usr/bin/vim'settings
if [ "${PS1-}"color_prompt" = yes ]; then
ifPS1='${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 "${BASH-}"([ $? = 0 ] && [echo terminal || echo error)" "$BASH"(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
# ~/bin/sh".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
# TheAuto-completion
file# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc alreadyand sets the default PS1./etc/profile
# PS1='\h:\w\$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.bashrcbash_completion ]; then
. /etc/bash.bashrcbash_completion
fi
elsefi
See below for more on each section
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 [ "`id$color_prompt" -u`"= -eq 0yes ]; then
PS1='#${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
#else
ThisPS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
There's two settins to configure, the first block allowsincludes forcolor, configuringthe second does not.
You can change this using any uservariety whosof idthe ==settings 0below. #After Inyou've othergot words,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 appliedlost, so no worries about getting back to default. This is a good way to test what would happen if you changed the rootPS1 userwithin only.your else.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 settingscolor willParrotsec
export inPS1='┌──˨\u@\h─[\w]\n└──╼\$`
all
Colorized cases,Parrotsec
export #PS1="\[\033[0;31m\]\342\224\214\342\224\200\$([[ In\$? other!= words,0 upon]] successful&& loginecho to an authroized user, this block will be executed
fi
fi
fi
\"[\[\033[0;31m\]\342\234\227\[\033[0;37m\]]\342\224\200\")[$(if [[ -d${EUID} /etc/profile.d== 0 ]]; then forecho i in /etc/profile.d/*.sh; do
if '\[ -r $i \033[01;31m\]root\[\033[01;33m\]@\[\033[01;96m\]\h'; 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
So, to recap setting system-wide bash settings that replicate for all users, both currently existing and those created in the future, modify the following files -
/etc/skel/.bashrcUsed as a template for new users when initializing a basic .bashrc in their home directory.
/etc/bash.bashrcThe system-wide bashrc for interactive bash shells, invoked on any login to an interactive shell.
~/.bashrcThe user's local .bashrc configuration, used to specify settings for a specific user
/etc/profileUsed to assign system-wide settings for profile related conditionals.ex. If UID=0 (root) then;ex.elsethen;echo
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
When a bash profile is not placed in the home directory, the default profile is loaded from /etc/profile. 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.
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if \[ "\033[0m\]\[\e[01;33m\]\\${PS1-}\[\e[0m\]"
]; 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='# '
else
PS1='$ '
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
GeneralBash SyntaxOptions
$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)
$PS1 : Your shell prompt variable.
\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 $.
Followingwe can create a simple export like the codes below and syntax above,Toss this escape code inbelow, to colorizetest everythingthese greenvalues after:before \e[\[0;32m\]adding any color
Moreexport
colorPS1='${debian_chroot:+($debian_chroot)}FQDN: codes:
More bash 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.
\] This sequence should appear after a sequence of non-printing characters.
Misuse of escape sequences \[
and \]
-. Cannot use symbols directly in .bashrc:
bashrc -
\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\]╼$
Translating Symbols for use in .bashrc
1. Search up a symbol to get UTF8 - https://www.compart.com/en/unicode/search?q=#characters
ഽ = 0xE0 0xB4 0xBD
2. Take the UTFx8 encoding and run it through wolfram -https://www.wolframalpha.com/
(Separate by commas, clean up spacing first: 0xE0, 0xB4, 0xBD)
0xE0, 0xB4, 0xBD = \340\264\275
3. Depending on encoding of the system (LANG=US.utf8), take the base conversion into bashrc or export command:
export PS1="\340\264\275"
returns ഽ
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$
Color codes
Using the appropriate bash syntax and following the codes below,
Toss this escape code in to colorize everything green after:
\e[\[0;32m\]
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 |
Translating Symbols for use in .bashrc
1. Search up a symbol to get UTF8 - https://www.compart.com/en/unicode/search?q=#characters
ഽ = 0xE0 0xB4 0xBD
2. Take the UTFx8 encoding and run it through wolfram -https://www.wolframalpha.com/
(Separate by commas, clean up spacing first: 0xE0, 0xB4, 0xBD)
0xE0, 0xB4, 0xBD = \340\264\275
3. Depending on encoding of the system (LANG=US.utf8), take the base conversion into bashrc or export command:
export PS1="\340\264\275"
returns ഽ
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.
Migrating ExportBash ExamplesSettings
AnyThe following block contains a listing of bash profiles, locations, and their 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_logout
The individual login shell cleanup file, executed when a login shell exits
~/.inputrc
Individual readline initialization file
So, setting system-wide bash settings that replicate for this and all future users, look to modify the belowfollowing exportsfiles -
- /etc/skel/.bashrc
- Used as a template for new users when initializing a basic .bashrc in their home directory.
- /etc/bash.bashrc
- The system-wide bashrc for interactive bash shells, invoked on any login to an interactive shell.
- ~/.bashrc
- The user's local .bashrc configuration, used to specify settings for a specific user
- /etc/profile
- Used to assign system-wide settings for profile related conditionals.
- ex. If UID=0 (root) then;
- ex. else then;
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 pastedcreated directlyor intoreconfigured, depending on your current setup. The page below will show some basic syntax for editing the terminalfile, along with some examples. The default bash profile can be seen in the code block below.
In short, to beset tested.a Oncesystem-wide variable, such as EDITOR='/usr/bin/vim'
, simply add the terminalfollowing isline closed,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 lost, so no worries about getting backapplied to default.the Noroot coloruser Parrotsec:
exportonly.
else
PS1='┌──˨\u@\h─[\w]\n└──╼\$
'
Full
Parrot:
# exportThese PS1="\[\033[0;31m\]\342\224\214\342\224\200\$([[settings \$?will !=apply 0in ]]all &&other echocases, \"[\[\033[0;31m\]\342\234\227\[\033[0;37m\]]\342\224\200\")[$(system-wide
# In other words, upon successful login to an authroized user, this block will be executed
fi
fi
fi
if [[ ${EUID}-d ==/etc/profile.d 0 ]]; then
echofor '\i in /etc/profile.d/*.sh; do
if [\033[01;31m\ -r $i ]root\[\033[01;33m\]@\[\033[01;96m\]\h'; elsethen
echo. '\[\033[0;39m\]\u\[\033[01;33m\]@\[\033[01;96m\]\h';$i
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\274fi
\[\033[0m\]\[\e[01;33m\]\\$\[\e[0m\]"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