Creating a custom bash profile
The following block contains a listing of bash profiles, locations, and their use.
/bin/bash
The bash executable
/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, 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'
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
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 [ "${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='# '
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
General Syntax
\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 base of current working directory.
\$: Display # (indicates root user) if the effective UID is 0, otherwise display a $.
Following the codes below and syntax above,
Toss this escape code in to colorize everything green after:
\e[\[0;32m\]
More color codes:
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 |
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:
\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$
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.
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.
No color Parrotsec:export PS1='┌──˨\u@\h─[\w]\n└──╼\$
'
Full Parrot:
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\]"