You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.7 KiB
Bash

5 years ago
#!/usr/bin/env bash
# shellcheck disable=SC2230
5 years ago
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: circa 2006 (forked from .bashrc)
#
2 years ago
# https://github.com/HariSekhon/DevOps-Bash-tools
5 years ago
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
2 years ago
# https://www.linkedin.com/in/HariSekhon
5 years ago
#
# ============================================================================ #
# E n v i r o n m e n t V a r i a b l e s
# ============================================================================ #
5 years ago
# more environment variables defined next to the their corresponding aliases in aliases.sh
bash_tools="${bash_tools:-$(dirname "${BASH_SOURCE[0]}")/..}"
5 years ago
# shellcheck disable=SC1090,SC1091
. "$bash_tools/.bash.d/os_detection.sh"
5 years ago
5 years ago
# similar to what zsh does by default
if [ -f ~/.bashenv ]; then
# shellcheck disable=SC1090,SC1091
4 years ago
. ~/.bashenv
5 years ago
fi
5 years ago
#export DISPLAY=:0.0
5 years ago
#export TERM=xterm
5 years ago
export EDITOR=vim
5 years ago
export INPUTRC=~/.inputrc
4 years ago
# allow programs to use $LINES and $COLUMNS
5 years ago
export LINES
4 years ago
export COLUMNS
5 years ago
7 months ago
# sets directories to cyan on default bg so they stand out more in dark terminal - see 'man ls' for more details
7 months ago
# works on Mac - you may need to see 'man 5 dir_colors' on Linux
7 months ago
export LSCOLORS="gx"
5 years ago
# ENV refers to the file that sh attempts to read as a startup file (done on my Mac OSX Snow Leopard)
# Needs the following line added to sudoers for ENV to be passed through on sudo su
#Defaults env_keep += "ENV"
5 years ago
export ENV=~/.bashrc
5 years ago
7 months ago
# ============================================================================ #
# L o c a l e I n t e r n a t i o n a l i z a t i o n
# ============================================================================ #
# Run this to see available locales:
#
# locale -a
7 months ago
#
# See details of a specific locale variable eg. time formats:
#
# LC_ALL=C locale -ck LC_TIME
7 months ago
5 years ago
# aterm doesn't support UTF-8 and you get horrible chars here and there
# so don't use utf and aterm together. xterm works ok with utf8 though
#export LANG=en_GB
7 months ago
#
# LANG becomes default value for any LC_xxx variables not set
5 years ago
#export LANG=C
7 months ago
#
# overrides all other LC_xxx variables
5 years ago
#export LC_ALL=C
7 months ago
#
5 years ago
export LANG=en_US.UTF-8
5 years ago
export LC_ALL=en_US.UTF-8
7 months ago
export LANGUAGE=en_US.UTF-8
#export LC_ALL=en_GB
5 years ago
# didn't seem to work
5 years ago
#export LANG="en_GB.UTF-8"
#export LC_ALL="en_GB.UTF-8"
7 months ago
# ============================================================================ #
# Clever dynamic environment variables, set using var() function sourced between shells
5 years ago
export varfile=~/.bash_vars
# shellcheck disable=SC1090,SC1091
[ -f "$varfile" ] && . "$varfile"
8 months ago
# Secret Credentials
#
# separate cred files so if you accidentally expose it on a screen
# to colleagues or on a presentation or screen share
# you don't have to change all of your passwords
# which you would have to if using the above ~/.bash_vars file
if [ -d ~/.env/creds ]; then
for credfile in ~/.env/creds/*; do
if [ -f "$credfile" ]; then
# shellcheck disable=SC1090,SC1091
. "$credfile"
fi
done
fi
5 years ago
#export DISTCC_DIR="/var/tmp/portage/.distcc/"
5 years ago
# ============================================================================ #
if is_mac; then
5 years ago
#BROWSER=open
unset BROWSER
elif type -P google-chrome &>/dev/null; then
5 years ago
BROWSER=google-chrome
elif type -P firefox &>/dev/null; then
5 years ago
BROWSER=firefox
elif type -P konqueror &>/dev/null; then
5 years ago
BROWSER=konqueror
5 years ago
elif [ -n "${GOOGLE_CLOUD_SHELL:-}" ]; then
:
5 years ago
else
4 years ago
:
#BROWSER=UNKNOWN
#echo "COULD NOT FIND ANY BROWSER IN PATH"
5 years ago
fi
# don't export BROWSER on Mac, trigger python bug:
# AttributeError: 'MacOSXOSAScript' object has no attribute 'basename'
# from python's webbrowser library
if ! is_mac; then
5 years ago
export BROWSER
fi
var(){
local var="${*%%=*}"
local val="${*#*=}"
if grep -i "export $var" "$varfile" &>/dev/null; then
perl -pi -e 's/^export '"$var"'=.*$/export '"$var"'='"$val"'/' "$varfile"
else
echo "export $var=$val" >> "$varfile"
fi
export "$var"="$val"
}
vars(){
5 years ago
"$EDITOR" "$varfile"
4 years ago
chmod 0600 "$varfile"
# shellcheck disable=SC1090,SC1091
5 years ago
. "$varfile"
}
unvar(){
local var="${*%%=*}"
[ -f "$varfile" ] || { echo "$varfile not found" ; return 1; }
perl -pi -e 's/^export '"$var"'=.*\n$//' "$varfile"
unset "$var"
}
4 months ago
# ============================================================================ #
unsetall(){
local match="${1:-.*}"
while read -r env_var; do
if [ "$env_var" = PATH ]; then
continue
fi
unset "$env_var"
done < <( env |
grep -i "$match" |
sed 's/=.*//' )
}