1 # shellcheck disable=SC1090 disable=SC1091 # -- Can't follow non-constant source
2 # shellcheck disable=SC1003 # -- Don't complain about '\e\\'
4 ###########################
5 # Settings for all shells #
6 ###########################
8 # Set umask, depending on the user ID
9 if [ "$(id -un)" = "root" ]; then
15 # Add ~/bin and subdirs to PATH if needed
17 echo "$PATH" | tr ':' '\n' | grep -qxF "$p" || PATH="${p}:$PATH"
18 done < <(find -L ~/bin -depth -maxdepth 1 -type d 2> /dev/null)
20 # Set environment variables, eg EMAIL, LANG
21 eval "$(cat 2> /dev/null /etc/default/locale /etc/locale.conf ~/.config/environment.d/*.conf | awk -F= '/^[^# ]/ {print $0 ";export " $1}')"
23 # Pick up SSH agent socket
24 if [ -z "$SSH_AUTH_SOCK" ]; then
25 if [ -r ~/.ssh-agent.env ]; then
26 source ~/.ssh-agent.env
28 SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh"
33 # Look for XDG_RUNTIME_DIR
34 if [ -z "$XDG_RUNTIME_DIR" ] && [ -d "/run/user/$(id -u)" ]; then
35 XDG_RUNTIME_DIR="/run/user/$(id -u)"
36 export XDG_RUNTIME_DIR
40 if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && [ -S "/run/user/$(id -u)/bus" ]; then
41 DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
42 export DBUS_SESSION_BUS_ADDRESS
46 ###########################
47 # Interactive shells only #
48 ###########################
50 if [ -z "${PS1}" ]; then
59 # Debian/Ubuntu don't source this from /etc/bash.bashrc
60 [ -z "$BASH_COMPLETION_VERSINFO" ] && [ -r /usr/share/bash-completion/bash_completion ] && source /usr/share/bash-completion/bash_completion
62 # This isn't sourced for interactive shells on Debian/Ubuntu/Arch
63 # https://bugzilla.gnome.org/show_bug.cgi?id=697475
64 [ -r /etc/profile.d/vte.sh ] && source /etc/profile.d/vte.sh
71 # Bash should check the terminal size after every command terminates
74 # Don't attempt to tab-complete an empty line
75 shopt -s no_empty_cmd_completion
77 # Prevent overwriting existing files on stdout redirection
83 export HISTCONTROL='erasedups:ignoredups:ignorespace'
84 export HISTSIZE='100000'
85 export HISTTIMEFORMAT='%F %T '
100 # Disable CTRL-s / CTRL-q
103 # Use dark background colors in apps like vim
104 export COLORFGBG='15;0'
110 # Revision control status for git, hg, svn
112 function find_up_exists {
114 while [ -n "$d" ]; do
115 if [ -e "$d/$1" ]; then
123 for p in /usr/lib/git-core/git-sh-prompt \
124 /usr/share/git/completion/git-prompt.sh \
125 /usr/share/git-core/contrib/completion/git-prompt.sh \
126 /usr/local/share/git-core/contrib/completion/git-prompt.sh
128 [ -r "$p" ] && source "$p" && break
131 function my_git_ps1 {
132 find_up_exists .git || return
133 GIT_PS1_SHOWDIRTYSTATE=1 \
134 GIT_PS1_SHOWUNTRACKEDFILES=1 \
135 __git_ps1 ' (%s)' 2> /dev/null
139 find_up_exists .hg || return
141 status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
145 function my_svn_ps1 {
146 find_up_exists .svn || return
148 status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
149 [ -n "$status" ] && echo -n " ($status)"
154 PS1="$PS1"'\[\e[01;31m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]'
155 PS1="$PS1"'\[\e[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\e[00m\]'
159 #################################
160 # Display return codes on error #
161 #################################
163 function print_exit_code {
165 if [ -z "${BASH_SOURCE[1]}" ]; then
166 printf '\e[01;33mexit code: %s\e[00m\n' \
170 trap print_exit_code ERR
178 # This prevents long/ugly command lines from showing up in xterm titles
180 eval "function $1 { $2 \"\$@\"; }"
184 ##########################
185 # ls aliases and colours #
186 ##########################
188 if command -v dircolors &> /dev/null; then
191 LS_COLORS+=':ow=30;42'
194 export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
198 if ls --color=auto -v &> /dev/null; then
199 alias ls='ls --color=auto -v'
200 elif gls --color=auto -v &> /dev/null; then
201 alias ls='gls --color=auto -v'
202 elif ls -G &> /dev/null; then
216 alias _psresources='ps -wAo pid,user,%cpu,%mem,stat,start,time,args'
217 if [ "$(uname)" = "Linux" ]; then
218 aliasf pscpu '_psresources --sort -%cpu|less -S'
219 aliasf psmem '_psresources --sort -%mem|less -S'
220 aliasf pstree 'ps --forest -weo pid,user:16,args|less -S'
221 aliasf pstime 'ps -wAo pid,user,lstart,args --sort start_time|less -S'
223 aliasf pscpu '_psresources -r|less -S'
224 aliasf psmem '_psresources -m|less -S'
225 aliasf pstime 'ps -wAo pid,user,lstart,args|less -S'
232 alias f='find . -iname'
233 if echo x | grep -q --color=auto x &> /dev/null; then
234 alias grep='grep --color=auto'
238 alias watch='watch -n1'
239 alias sudo='sudo ' # ability to use aliases with sudo
240 aliasf sudosu 'sudo su -l -s /bin/bash'
241 aliasf python 'PYTHONSTARTUP=~/.pythonrc.py python3'
242 aliasf webshare 'python3 -mhttp.server'
245 aliasf edit "$EDITOR"
247 # man with coloured headings and a terminal title
250 LESS_TERMCAP_md=$'\E[01;38;5;74m' \
251 LESS_TERMCAP_me=$'\E[0m' \
252 LESS_TERMCAP_us=$'\E[04;38;5;146m' \
253 LESS_TERMCAP_ue=$'\E[0m' \
258 # Creates the directory if it doesn't exist, and changes into it
260 # shellcheck disable=SC2164
261 mkdir -p "$1" && cd "$1"
264 # Sets the nice and ionice priorities for the current shell to the lowest values
270 # SSH without verifying host key
271 function ssh_unsafe {
272 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
276 #########################
277 # Optional local config #
278 #########################
280 [ -r ~/.bashrc_local ] && source ~/.bashrc_local
283 ########################
284 # Terminal integration #
285 ########################
287 # When at a prompt display `workingdir (hostname)`
288 function print_title_prompt {
289 printf '\e]0;bash:%s (%s)\a' \
291 "$(cat /etc/hostname)"
293 if [[ "$TERM" == screen* ]]; then
298 # Preserve working directory when opening new terminals
299 # This depends on /etc/profile/vte.sh
300 function record_terminal_cwd {
301 [ "$(type -t __vte_osc7)" = "function" ] && __vte_osc7 || true
304 function prompt_command {
308 PROMPT_COMMAND=prompt_command
310 # Display the command about to be executed. This must go at the end of the
311 # bashrc to avoid running the trap on commands in the bashrc
312 function print_title_exec {
313 [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return
314 [ "$BASH_COMMAND" = "__vte_osc7" ] && return
316 printf '\e]0;%s (%s)\a' \
317 "$(tr -cd '[:graph:] ' <<< "$BASH_COMMAND")" \
318 "$(cat /etc/hostname)"
320 if [[ "$TERM" == screen* ]]; then
322 "$(sed -n -e 's/sudo //' -e 's/ .*//' -e 1p <<< "$BASH_COMMAND")"
325 trap print_title_exec DEBUG