]> arthur.barton.de Git - ConfigScripts.git/blob - sys/bashrc
Make check for "interactive shells" more robust
[ConfigScripts.git] / sys / bashrc
1 #
2 # /etc/bash.bashrc: System-wide rc file for interactive bash(1) shells.
3 # Written 2003-2014 by Alexander Barton (alex@barton.de)
4 #
5
6 [ -e ~/.ConfigScripts.debug ] && echo " .. /etc/bash.bashrc ..."
7
8 BASHRCREAD="true"
9
10 [ -z "$PROFILEREAD" -a -r /etc/profile ] && source /etc/profile
11 [ -z "$PROFILEREAD" -a -r ~/.etc/profile ] && source ~/.etc/profile
12
13 [ -e ~/.ConfigScripts.debug ] && echo " >> /etc/bash.bashrc ..."
14
15 # Interactive shell?
16 case "$-" in
17         *i*) ;;
18         *) return ;;
19 esac
20
21 # Initialize keychain(1) ssh-agent and gpg-agent helper
22 type -t keychain >/dev/null \
23         && eval `keychain --agents ssh --eval --quick --quiet`
24
25 # Common command aliases (1/2)
26 alias ".."="cd .."
27 alias "ls"="ls -F"
28 alias "ll"="ls -l"
29 alias "l"="ll -a"
30
31 # Shell options (1/2)
32 set mark-directories on
33 set mark-symlinked-directories on
34 HISTCONTROL=ignoredups
35 HISTSIZE=500
36 HISTFILESIZE=2000
37
38 # Shell prompt
39 PS1="\u@\h:\w \$ "
40
41 # Make sure that the following commands are only run with bash >= 2.x
42 case "$BASH_VERSION" in
43         "0."*|"1."*) return; ;;
44         *)
45 esac
46
47 # Shell options (2/2)
48 shopt -s checkwinsize
49 shopt -s cdspell
50 shopt -s histappend
51 shopt -s histreedit
52 shopt -s histverify
53
54 # Shell functions
55 sshnew() {
56         ssh -o "StrictHostKeyChecking no" "$@"
57 }
58 sshtmp() {
59         ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" "$@"
60 }
61
62 # Map up & down to history search once a command has been started
63 bind '"\e[A":history-search-backward'
64 bind '"\e[B":history-search-forward'
65
66 # Colors
67 case "$TERM" in
68         ansi|cons25|cygwin|dtterm|linux|rxvt|screen*|vt100|vt200|vt220|vt320| \
69         xterm|xterm-color|xterm-256color)
70                 # color terminal
71
72                 # terminal attributes
73                 #   0: reset / 1: bright / 2: dim / 4: underline / 5: blink /
74                 #   7: reverse / 8: hidden.
75                 # foreground colors
76                 #   30: black / 31: red / 32: green / 33: yellow / 34: blue /
77                 #   35: magenta / 36: cyan / 37: white
78                 # background colors
79                 #   40: black / 41: red / 42: green / 43: yellow / 44: blue /
80                 #   45: magenta / 46: cyan / 47: white
81
82                 # colors for shell prompt etc.
83                 COLOR_RESET="\[\e[0m\]"
84                 COLOR_PREPOSTTXT="\[\e[1;37m\]"
85                 COLOR_USER="\[\e[0;4m\]"
86                 COLOR_AT="\[\e[0;37m\]"
87                 COLOR_HOST="\[\e[0;4m\]"
88                 COLOR_COLON="\[\e[0;37m\]"
89                 COLOR_PATH=$reset
90                 COLOR_PROMPT_USER="\[\e[1;32m\]"
91                 COLOR_PROMPT_ROOT="\[\e[1;31m\]"
92                 COLOR_VCS="\[\e[1;33m\]"
93                 COLOR_HISTORY=$reset
94
95                 # colors for less(1) pager
96                 export LESS_TERMCAP_mb=$'\E[1;33m'
97                 export LESS_TERMCAP_md=$'\E[1;31m'
98                 export LESS_TERMCAP_me=$'\E[0m'
99                 export LESS_TERMCAP_se=$'\E[0m'
100                 export LESS_TERMCAP_so=$'\E[1;33;44m'
101                 export LESS_TERMCAP_ue=$'\E[0m'
102                 export LESS_TERMCAP_us=$'\E[1;32m'
103 esac
104
105 # Enable bash completion, if available
106 if ! shopt -oq posix; then
107         [ -z "$BASH_COMPLETION" -a -r /etc/bash_completion ] \
108                 && source /etc/bash_completion
109         [ -z "$BASH_COMPLETION" -a -r /usr/local/etc/bash_completion ] \
110                 && source /usr/local/etc/bash_completion
111         [ -z "$BASH_COMPLETION" -a -r /opt/homebrew/etc/bash_completion ] \
112                 && source /opt/homebrew/etc/bash_completion
113         [ -z "$BASH_COMPLETION" -a -r /opt/local/etc/bash_completion ] \
114                 && source /opt/local/etc/bash_completion
115 fi
116
117 # Enable local virtualenvwrapper installation, if available
118 [ -r /usr/local/bin/virtualenvwrapper.sh ] \
119         && source /usr/local/bin/virtualenvwrapper.sh
120
121 # Shell prompt
122 PS1="${COLOR_PREPOSTTXT}${PS1_BEGIN:-<}"
123 [ "$LOGNAME" = "root" ] \
124         || PS1="${PS1}${COLOR_USER}\u${COLOR_AT}@"
125 PS1="${PS1}${COLOR_HOST}\h"
126 PS1="${PS1}${COLOR_COLON}:"
127 PS1="${PS1}${COLOR_PATH}\W "
128 PS1="${PS1}${COLOR_HISTORY}\!"
129 PS1="${PS1}${COLOR_PREPOSTTXT}${PS1_END:->}${COLOR_RESET} "
130 [ "$LOGNAME" = "root" ] \
131         && PS1="${PS1}${COLOR_PROMPT_ROOT}\\\$${COLOR_RESET} " \
132         || PS1="${PS1}${COLOR_PROMPT_USER}\\\$${COLOR_RESET} "
133 type -t __git_ps1 >/dev/null \
134         && PS1="\$(__git_ps1 '(${COLOR_VCS}%s${COLOR_RESET}) ')${PS1}"
135 PS1="${chroot_name:+[${COLOR_CHROOT}$chroot_name${COLOR_RESET}] }${PS1}"
136 PS1="${COLOR_RESET}${PS1}"
137
138 # If the command-not-found package is installed, use it
139 if [ -r /etc/bash_command_not_found ]; then
140         . /etc/bash_command_not_found
141 elif [ -x /usr/lib/command-not-found ]; then
142         function command_not_found_handle {
143                 # Check because c-n-f could've been removed in the meantime
144                 if [ -x /usr/lib/command-not-found ]; then
145                         /usr/bin/python /usr/lib/command-not-found -- $1
146                         return $?
147                 else
148                         return 127
149                 fi
150         }
151 fi
152
153 # Common command aliases (2/2)
154 alias "lasth"="last | head -n \`expr \\\$LINES - 2\`"
155 alias "lastf"="last | grep -v \"^\$LOGNAME\""
156
157 # Aliases for screen(1), if installed
158 type -t screen >/dev/null
159 if [ $? -eq 0 ]; then
160         alias "s"="screen"
161         alias "sr"="screen -dr"
162         alias "sx"="screen -x"
163 fi
164
165 # less(1) filter, if available
166 type -t lesspipe >/dev/null \
167         && eval `lesspipe`
168
169 # Setup slrn(1) and cleanscore(1), if installed
170 type -t cleanscore >/dev/null \
171         && alias slrn="cleanscore -f ~/.slrnscore && slrn"
172
173 # Enable color support of ls(1)
174 if [ "$TERM" != "dumb" ]; then
175         type -t dircolors >/dev/null && eval `dircolors -b`
176         ls --color / >/dev/null 2>&1
177         if [ $? -eq 0 ]; then
178                 # "GNU style"
179                 alias ls="ls --color=auto -F"
180         else
181                 ls -G / >/dev/null 2>&1
182                 if [ $? -eq 0 ]; then
183                         # "BSD style"
184                         alias ls="ls -FG"
185                 fi
186         fi
187 fi
188
189 # If this is an xterm set the title to user@host:dir
190 case $TERM in
191 xterm*|rxvt|screen)
192         PROMPT_COMMAND='printf "\033]0;${LOGNAME}@${HOSTNAME}: ${PWD}\007"'
193         ;;
194 *)
195         ;;
196 esac
197
198 # Source local files, if readable
199 [ -r "/etc/bashrc.local" ] && source /etc/bashrc.local
200 [ -r "/etc/bash.bashrc.local" ] && source /etc/bash.bashrc.local
201 [ -r "/var/lib/$HOSTNAME/bash.bashrc" ] && source /var/lib/$HOSTNAME/bash.bashrc
202
203 # -eof-