F5F Stay Refreshed Software Operating Systems BashrcCollection: Tips, hacks, and small changes for better performance and experience

BashrcCollection: Tips, hacks, and small changes for better performance and experience

BashrcCollection: Tips, hacks, and small changes for better performance and experience

M
mydoodes
Junior Member
3
01-24-2023, 02:00 PM
#1
HI the purpose of this topic is to collect as many as possible bash improvements and set t ings that live in your bash running config( ~/.bashrc*0 A few rules beforehand: - every thing posted should atleast a description for what it does - and the depencies it may or may not have if they arend part of coreutils and or the posix std. - !!!! NO FUNCTIONS !!! if you want to share a neat function , and its not user specific , you can still post it but make sure it can operate as - a stand alone shell script in /opt/bin/ or /usr/bin/ or allow for dropping in /etc/profile.d or /etc/bash/bashrc.d/ without requiring anyting more than a simple call from bashrc or a source instruction - please also separate the aliasses that are distro specific , into a seperate code block so people can copy whole sections without cluttering their env wit aliases that wont work on their system... - if you do need to store something in the env please try to use a bash array ifits more than a single variable, and prefix the variable with the corename of the thing. - for any code that creates new files on the system a procedure to remove all those files automaticly needs to be included , even as a part of an istaller or part of the make file ... eg -dont assume people have sudo installed just write the thing as if ran by root , if it fails when run as regular user , not a problem but an error complaining about permissions is easier fixed than an error about file not found. ill start of with my discoveries some of them really old : 2004+ some of them only recent additions and i wish i had known since the beginning. (if possible i will correct any mistakes and also try keeping this list up to date with replies) note there is nothing about history control inhere , as i have written a seperate script to deal with bashhistory and logging that. i will post this section on the blog on my profile ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Intro: in bash there are some special variables that are worth knowing about: $$ = the PID of the current instance $_ = the last argument fromp previous comand mkdir -p ~/some/long/complicated/path/ cd $_ $@ stores all arguments passed to a script $? stores the return value of last executed ommand (use this to test if it failed or was success) $PS1 is the prompt that gets printd after each command $COLLUMS stores the width of the curent shell $LINES stores the height section: start of file ______________________________________________________________________________________________________________________________________________________ #!/usr/bin/env bash # ############################################################################ # # REPO: {Gitrepo} AUTHOR: {Author contact info} # # FILE: {intended filename} {last upd/version} # ############################################################################ # # Dont do anything if not interactivel: [[ $ - != * i * ]] && return X11 only: # Share X Server With All Users \xhost + > /dev/ null 2 >& 1 Systemd only: # Load Profile Again when chrooting: [[ "$UID" == 0 ]] && $ ( systemd - detect - virt - r ) && source / etc / profile || : extra info : ----------- xhost + : turns of access control to the xserver this is needed for xnest or xephir (2ways to run a desktop inside a window.) detect-virt () : systemd users only: useless for other init systems this systemd-detect-virt -r will exit with code 0 if inside chroot, if we are ,need to also source /etc/profile again section : bash options # Bash won't get SIGWINCH if another process is in the foreground. # Enable checkwinsize so that bash will check the terminal size when # it regains control. #65623 # http://cnswww.cns.cwru.edu/~chet/bash/FAQ (E11) shopt - s checkwinsize # Disable completion when the input buffer is empty. i.e. Hitting tab and waiting a long time for bash to expand all of $PATH. # shopt -s no_empty_cmd_completion # extended globbing shopt - s extglob # expand aliasses shopt - s expand_aliases # enable * wildcard includes .-files (ex: rm ~/tmp/* removes .test and test |fix for .* wich includes ..) shopt - s dotglob # reedit a history substitution line if it failed shopt - s histreedit # edit a recalled history line before executing shopt - s histverify # Enable history appending instead of overwriting. #139609 shopt - s histappend # change to named directory shopt - s autocd # change directory to variables shopt - s cdable_vars # autocorrects cd misspellings shopt - s cdspell # save multi-line commands in history as single line shopt - s cmdhist # no Case globbing **1 shopt - s nocaseglob Section: keybindings : # # PgUp/PgDn go to the beginning/end of the history # # Use the up and down arrow keys for finding a command in history # # (you can write some initial letters of the command first). bind '"\e[A":history-search-backward' bind '"\e[B":history-search-forward' bind '"\e[5~":beginning-of-history' bind '"\e[5~":end-of-history' bind 'set completion-ignore-case on' #[A is up arrow #[B is down arrow #[5~ is pgup #[6~ is pgdn Section : exports : # Default Editor: export EDITOR = "/usr/bin/env nvim" # PYTHON export PYTHONPATH = "${PYTHONPATH}:${HOME}/Development/Code/Python/" #if any replace this where you store git export PYTHONPATH = "${PYTHONPATH}:${HOME}/Development/Code/Python/Projects/" #python code and or homebrew projects Nvidia gfx card owners Only: # NVIDIA ## NVIDIA Vulkan export VK_ICD_FILENAMES = "/usr/share/vulkan/icd.d/nvidia_icd.json" # MANPAGER # Bat as Default Manpager export MANPAGER = "sh -c 'col -bx | bat -l man -p'" Spoiler t #colorize Manpages #if bat is not the default manpager, make them look a litte better: export LESS_TERMCAP_mb = $ '\e[1;32m' export LESS_TERMCAP_md = $ '\e[1;32m' export LESS_TERMCAP_me = $ '\e[0m' export LESS_TERMCAP_se = $ '\e[0m' export LESS_TERMCAP_so = $ '\e[01;33m' export LESS_TERMCAP_ue = $ '\e[0m' export LESS_TERMCAP_us = $ '\e[1;4;31m' #LSCOLORS (colors for the ls command) dircolors - b 411 _dir . colors LS_COLORS = 'rs=0:di=01;34:ln=02;37:mh=00:pi=40;33Confusedo=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=00;31;01:mi=00Confusedu=37;41Confusedg=30;43:ca=30;41:tw=30;42:ow=34;40Confusedt=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:' ; export LS_COLORS use_color = true Spoiler alias rebash = 'source ~/.bash_profile' alias ll = 'ls -hals' alias llr = 'ls -Rhals' alias frqmax = 'sudo cpupower frequency-set -g performance' alias frqmin = 'sudo cpupower frequency-set -g powersave' alias frqinfo = 'sudo cpupower frequency-info' alias cp = "cp -iv" # confirm before overwriting something alias df = 'df -h' # human-readable sizes alias free = 'free -m' # show sizes in MB alias np = 'micro -w PKGBUILD' alias more = less alias du = 'du -hc' alias yas = 'yay -Ss' alias yayy = 'yay --noconfirm' alias Bdu = 'sudo btrfs file du --human-readable -s' alias Bdf = '' alias BS = 'sudo btrfs subv' #= btrfs subvolume $ alias lsBS = 'sudo btrfs subv list' #= btrfs subvolume list $ alias ddBS = 'sudo btrfs subv snapshot' #= btrfs subvolume duplicate $ alias mkBS = 'sudo btrfs subv create' #= btrfs subvolume list $ alias rmBS = 'sudo btrfs subv delete' #= btrfs subvolume list $ alias BF = 'sudo btrfs file' #= btrfs filesystem $ alias pip = 'sudo pip' alias ntfxall = "sudo lsblk -o PATH,FSTYPE | awk '/ntfs/ {print $1}'| xargs -n1 sudo ntfsfix -d" # if eza is installed : alias ls = 'eza --icons' #list alias ls = 'ls --color=auto' alias la = 'ls -a' #alias ll='ls -la' alias ll = 'ls -Al' alias lsdot = "ls -A | egrep '^\.'" alias ls1 = "ls -1A" ## Colorize the grep command output for ease of use (good for log files)## alias grep = 'grep --color=auto' alias egrep = 'egrep --color=auto' alias fgrep = 'fgrep --color=auto' #readable output #alias df='df -h' #fix obvious typo's alias cd ..= 'cd ..' alias pdw = "pwd" alias udpate = 'sudo pacman -Syyu' alias upate = 'sudo pacman -Syyu' alias updte = 'sudo pacman -Syyu' alias updqte = 'sudo pacman -Syyu' alias upqll = "yay -Syu --noconfirm" alias upal = "yay -Syu --noconfirm" #ps alias psa = "ps auxf" alias psgrep = "ps aux | grep -v grep | grep -i -e VSZ -e" #free alias free = "free -mt" #use all cores alias uac = "sh ~/.bin/main/000*" #grub update alias update - grub = "sudo grub-mkconfig -o /boot/grub/grub.cfg" #add new fonts alias update - fc = 'sudo fc-cache -fv' #continue download alias wget = "wget -c" #userlist alias userlist = "cut -d: -f1 /etc/passwd"
M
mydoodes
01-24-2023, 02:00 PM #1

HI the purpose of this topic is to collect as many as possible bash improvements and set t ings that live in your bash running config( ~/.bashrc*0 A few rules beforehand: - every thing posted should atleast a description for what it does - and the depencies it may or may not have if they arend part of coreutils and or the posix std. - !!!! NO FUNCTIONS !!! if you want to share a neat function , and its not user specific , you can still post it but make sure it can operate as - a stand alone shell script in /opt/bin/ or /usr/bin/ or allow for dropping in /etc/profile.d or /etc/bash/bashrc.d/ without requiring anyting more than a simple call from bashrc or a source instruction - please also separate the aliasses that are distro specific , into a seperate code block so people can copy whole sections without cluttering their env wit aliases that wont work on their system... - if you do need to store something in the env please try to use a bash array ifits more than a single variable, and prefix the variable with the corename of the thing. - for any code that creates new files on the system a procedure to remove all those files automaticly needs to be included , even as a part of an istaller or part of the make file ... eg -dont assume people have sudo installed just write the thing as if ran by root , if it fails when run as regular user , not a problem but an error complaining about permissions is easier fixed than an error about file not found. ill start of with my discoveries some of them really old : 2004+ some of them only recent additions and i wish i had known since the beginning. (if possible i will correct any mistakes and also try keeping this list up to date with replies) note there is nothing about history control inhere , as i have written a seperate script to deal with bashhistory and logging that. i will post this section on the blog on my profile ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Intro: in bash there are some special variables that are worth knowing about: $$ = the PID of the current instance $_ = the last argument fromp previous comand mkdir -p ~/some/long/complicated/path/ cd $_ $@ stores all arguments passed to a script $? stores the return value of last executed ommand (use this to test if it failed or was success) $PS1 is the prompt that gets printd after each command $COLLUMS stores the width of the curent shell $LINES stores the height section: start of file ______________________________________________________________________________________________________________________________________________________ #!/usr/bin/env bash # ############################################################################ # # REPO: {Gitrepo} AUTHOR: {Author contact info} # # FILE: {intended filename} {last upd/version} # ############################################################################ # # Dont do anything if not interactivel: [[ $ - != * i * ]] && return X11 only: # Share X Server With All Users \xhost + > /dev/ null 2 >& 1 Systemd only: # Load Profile Again when chrooting: [[ "$UID" == 0 ]] && $ ( systemd - detect - virt - r ) && source / etc / profile || : extra info : ----------- xhost + : turns of access control to the xserver this is needed for xnest or xephir (2ways to run a desktop inside a window.) detect-virt () : systemd users only: useless for other init systems this systemd-detect-virt -r will exit with code 0 if inside chroot, if we are ,need to also source /etc/profile again section : bash options # Bash won't get SIGWINCH if another process is in the foreground. # Enable checkwinsize so that bash will check the terminal size when # it regains control. #65623 # http://cnswww.cns.cwru.edu/~chet/bash/FAQ (E11) shopt - s checkwinsize # Disable completion when the input buffer is empty. i.e. Hitting tab and waiting a long time for bash to expand all of $PATH. # shopt -s no_empty_cmd_completion # extended globbing shopt - s extglob # expand aliasses shopt - s expand_aliases # enable * wildcard includes .-files (ex: rm ~/tmp/* removes .test and test |fix for .* wich includes ..) shopt - s dotglob # reedit a history substitution line if it failed shopt - s histreedit # edit a recalled history line before executing shopt - s histverify # Enable history appending instead of overwriting. #139609 shopt - s histappend # change to named directory shopt - s autocd # change directory to variables shopt - s cdable_vars # autocorrects cd misspellings shopt - s cdspell # save multi-line commands in history as single line shopt - s cmdhist # no Case globbing **1 shopt - s nocaseglob Section: keybindings : # # PgUp/PgDn go to the beginning/end of the history # # Use the up and down arrow keys for finding a command in history # # (you can write some initial letters of the command first). bind '"\e[A":history-search-backward' bind '"\e[B":history-search-forward' bind '"\e[5~":beginning-of-history' bind '"\e[5~":end-of-history' bind 'set completion-ignore-case on' #[A is up arrow #[B is down arrow #[5~ is pgup #[6~ is pgdn Section : exports : # Default Editor: export EDITOR = "/usr/bin/env nvim" # PYTHON export PYTHONPATH = "${PYTHONPATH}:${HOME}/Development/Code/Python/" #if any replace this where you store git export PYTHONPATH = "${PYTHONPATH}:${HOME}/Development/Code/Python/Projects/" #python code and or homebrew projects Nvidia gfx card owners Only: # NVIDIA ## NVIDIA Vulkan export VK_ICD_FILENAMES = "/usr/share/vulkan/icd.d/nvidia_icd.json" # MANPAGER # Bat as Default Manpager export MANPAGER = "sh -c 'col -bx | bat -l man -p'" Spoiler t #colorize Manpages #if bat is not the default manpager, make them look a litte better: export LESS_TERMCAP_mb = $ '\e[1;32m' export LESS_TERMCAP_md = $ '\e[1;32m' export LESS_TERMCAP_me = $ '\e[0m' export LESS_TERMCAP_se = $ '\e[0m' export LESS_TERMCAP_so = $ '\e[01;33m' export LESS_TERMCAP_ue = $ '\e[0m' export LESS_TERMCAP_us = $ '\e[1;4;31m' #LSCOLORS (colors for the ls command) dircolors - b 411 _dir . colors LS_COLORS = 'rs=0:di=01;34:ln=02;37:mh=00:pi=40;33Confusedo=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=00;31;01:mi=00Confusedu=37;41Confusedg=30;43:ca=30;41:tw=30;42:ow=34;40Confusedt=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:' ; export LS_COLORS use_color = true Spoiler alias rebash = 'source ~/.bash_profile' alias ll = 'ls -hals' alias llr = 'ls -Rhals' alias frqmax = 'sudo cpupower frequency-set -g performance' alias frqmin = 'sudo cpupower frequency-set -g powersave' alias frqinfo = 'sudo cpupower frequency-info' alias cp = "cp -iv" # confirm before overwriting something alias df = 'df -h' # human-readable sizes alias free = 'free -m' # show sizes in MB alias np = 'micro -w PKGBUILD' alias more = less alias du = 'du -hc' alias yas = 'yay -Ss' alias yayy = 'yay --noconfirm' alias Bdu = 'sudo btrfs file du --human-readable -s' alias Bdf = '' alias BS = 'sudo btrfs subv' #= btrfs subvolume $ alias lsBS = 'sudo btrfs subv list' #= btrfs subvolume list $ alias ddBS = 'sudo btrfs subv snapshot' #= btrfs subvolume duplicate $ alias mkBS = 'sudo btrfs subv create' #= btrfs subvolume list $ alias rmBS = 'sudo btrfs subv delete' #= btrfs subvolume list $ alias BF = 'sudo btrfs file' #= btrfs filesystem $ alias pip = 'sudo pip' alias ntfxall = "sudo lsblk -o PATH,FSTYPE | awk '/ntfs/ {print $1}'| xargs -n1 sudo ntfsfix -d" # if eza is installed : alias ls = 'eza --icons' #list alias ls = 'ls --color=auto' alias la = 'ls -a' #alias ll='ls -la' alias ll = 'ls -Al' alias lsdot = "ls -A | egrep '^\.'" alias ls1 = "ls -1A" ## Colorize the grep command output for ease of use (good for log files)## alias grep = 'grep --color=auto' alias egrep = 'egrep --color=auto' alias fgrep = 'fgrep --color=auto' #readable output #alias df='df -h' #fix obvious typo's alias cd ..= 'cd ..' alias pdw = "pwd" alias udpate = 'sudo pacman -Syyu' alias upate = 'sudo pacman -Syyu' alias updte = 'sudo pacman -Syyu' alias updqte = 'sudo pacman -Syyu' alias upqll = "yay -Syu --noconfirm" alias upal = "yay -Syu --noconfirm" #ps alias psa = "ps auxf" alias psgrep = "ps aux | grep -v grep | grep -i -e VSZ -e" #free alias free = "free -mt" #use all cores alias uac = "sh ~/.bin/main/000*" #grub update alias update - grub = "sudo grub-mkconfig -o /boot/grub/grub.cfg" #add new fonts alias update - fc = 'sudo fc-cache -fv' #continue download alias wget = "wget -c" #userlist alias userlist = "cut -d: -f1 /etc/passwd"