This is going to be a long one... So much is (or can be) involved with a .bashrc file from the moment you open your terminal. My .bashrc has evolved from the basic default to a fairly complex file in which each line has a very specific purpose. I will break down each line and explain the reasoning, and what it does for me.
Since you (yeah you!) might be a seasoned Bashmaster or a Bashnewbee, I am going to break things down for the later. I was a Bashnewbee not too long ago and I still remember as I was learning, missing context that was assumed I had by the content provider (whether that was from a blog post or a YouTube video).
The .bashrc file
Location
absolute path: /home/{YOUR USER NAME}/.bashrc
relative path: ~/.bashrc
Hidden By Design
Since .bashrc begins with a period ., it is hidden from most 'default' views. For example, when you list files in a folder with the ls command, the 'default' view of the listed files excludes hidden files such as your .bashrc file. To see the hidden files you must add an optional flag (-a) to the ls command.
Here is an example of a purpose of your .bashrc file! Since, most of the time, when you want to view a list of files in a folder you want to see ALL of the files; you can make that happen in your .bashrc with an "alias".
#!/bin/bash
alias ls='ls -a'My .bashrc
Scroll through my .bashrc file below (you can scroll inside the monitor):
# !/bin/bash
# If not running interactively, don't do anything (leave this at the top of this file)
[[$- != *i*]] && return
# All the default Omarchy aliases and functions
source ~/.local/share/omarchy/default/bash/rc
# change refernces of specific programs' default locations(to get them out of home folder)
eval "$(SHELL=/bin/bash command try init ~/Documents/Work/tries)"
export XCOMPOSEFILE="$HOME/.config/.XCompose"
# Alias's to modified commands and abbreviated commands
alias cp='cp -i' # copies in interactive mode ( prompts user before overwriting )
alias mv='mv -i' # moves in interactive mode ( prompts user before overwriting )
alias rm='trash -v' # run trash program (like recycle bin) instead of actually deleting folders/files
alias ps='ps auxf' # run ps with flags that show a cool view of processes
alias ping='ping -c 4' # run ping only 4 times instead of default continuously
alias cls='clear && source ~/.bashrc' # easy way to clear terminal screen and re-source .bashrc
alias cat='bat ' # use bat in place of cat for all the benefits bat comes with
alias rdp='remmina' # open remmina (remote desktop client)
alias grep='rg' # use modern fast ripgrep instead of old slow grep
alias lg='lazygit'
# custom commands
alias ghkeys='ghostty +list-keybinds --default' # show Ghostty terminal keybinds
alias ipa='ip -br -4 addr' # show just short and sweet active interface IP's
alias netr='sudo systemctl restart systemd-networkd' # restart network service
alias todo='presenterm ~/.config/TODO.md'
alias todoedit='nvim ~/.config/TODO.md'
# Change directory aliases
alias cd~='cd ~'
alias cd..='cd ..'
alias ..='cd ..'
# Automatically do an ls after cd
cd() {
if [ -n "$1" ]; then
builtin cd "$@" && ls
else
builtin cd ~ && ls
fi
}
alias mkdir='mkdir -p'
# Automatically cd into directory after created
mkcd() {
mkdir -p -- "$1" && cd -- "$1"
}
# Alias's for directory listing commands
alias la='\eza --long --all --git --header --color=always --icons'
alias llg='eza --long --all --git --header --color=always --icons --no-permissions --no-user'
alias lt='\eza --long --all --git --header --tree --level=2 --color=always --icons | less -R'
alias lr='ls -lRh'
alias lm='ls -alh |more'
alias lw='ls -xAh'
alias ll='ls -als Name'
alias l='ls -als Name'
# open yazi with wrapper so that when yazi is closed, the cwd will be the last folder yazi was in
function y() {
local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd
command yazi "$@" --cwd-file="$tmp"
IFS= read -r -d '' cwd <"$tmp"
[ "$cwd" != "$PWD" ] && [ -d "$cwd" ] && builtin cd -- "$cwd"
rm -f -- "$tmp"
}
# open file(or folder) in neovim using fzf to select
ng() {
if [ -n "$1" ]; then # n $(rg --files --hidden | rg $1 | fzf) # -- 'fd --type f --hidden'
n $(fd --type f | rg $1 | fzf) # -- 'fd --type f --hidden'
else
echo "No filename pattern given. example: 'ng file1'"
fi
}
# open cheat.sh instead of tldr (since cht.sh shows tldr info also)
tldr() {
if [ -n "$1" ]; then
ghostty +new-window --wait-after-command -e bash -c "curl -s -S 'cht.sh/{$1}?T' | bat -p --language=bash"
else
echo "No command was given. example: 'tldr lua'"
fi
}
# add colors to man pages
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'
# enable vim motions in terminal bash prompt
set -o vi
# set path for .Net SDK's and Runtimes for dotnet development
export DOTNET_ROOT=/usr/share/dotnet
export PATH=/usr/share/dotnet:$PATH
export PATH=$PATH:$DOTNET_ROOT:$HOME/.dotnet/toolsNow I will explain each line (or block of lines).
# If not running interactively, don't do anything (leave this at the top of this file)
[[$- != *i*]] && returnConsider that every time you open a terminal, your .bashrc is "sourced" (applied to the terminal you opened). The above line checks to see if you opened the terminal "interactively" and if it is not, then stop processing the file immediately. What does "interactively" mean you may be asking?? Many times programs/applications are opened or ran "from" a terminal. For example a terminal running a GUI program or a terminal running a background process. These would be cases where you would not be "interacting" with the terminal itself and most likely the terminal itself would be hidden. In those cases, you do not want all the crap you have configured in your .bashrc to run in that terminal session.
[[$- != *i*]] is checking if the terminal is NOT being run interactively.
If the above is true:
&& return is ending the processing of the rest of the file.
# All the default Omarchy aliases and functions
source ~/.local/share/omarchy/default/bash/rcThis line is "sourcing" an additional bash file. Wait, it is just a file named rc!!! Yep, the name of the file does not matter. Any text file can contain valid "bash scripts" and furthermore the .bashrc file itself is simply a bash script file (however the name ".bashrc" is baked into the "bash shell" program which is spawned when your terminal is opened)
> Note: This entire post is referencing BASH as your shell program( vs other shells like ZSH or FISH )
So, that file rc, in my configuration, has a bunch of other bash scripting that comes out of the box with the Omarchy Linux distribution. Keep in mind that the order of your lines of script matter. In this case, after the scripts have been processed that are in that rc file, everything after that line in my .bashrc can override what was processed in that rc file.
You can add as many source ~/mycoolbash_script as you want. And place them in your .bashrc where it makes sense for them to run (or conditionally).
# Alias's to modified commands and abbreviated commands
alias cp='cp -i' # copies in interactive mode ( prompts user before overwriting )
alias mv='mv -i' # moves in interactive mode ( prompts user before overwriting )
alias rm='trash -v' # run trash program (like recycle bin) instead of actually deleting folders/files
alias ps='ps auxf' # run ps with flags that show a cool view of processes
alias ping='ping -c 4' # run ping only 4 times instead of default continuously
alias cls='clear && source ~/.bashrc' # easy way to clear terminal screen and re-source .bashrc
alias cat='bat ' # use bat in place of cat for all the benefits bat comes with
alias rdp='remmina' # open remmina (remote desktop client)
alias grep='rg' # use modern fast ripgrep instead of old slow grep
alias lg='lazygit'This block should be self explanatory with the inline comments.
# custom commands
alias ghkeys='ghostty +list-keybinds --default' # show Ghostty terminal keybinds
alias ipa='ip -br -4 addr' # show just short and sweet active interface IP's
alias netr='sudo systemctl restart systemd-networkd' # restart network service
alias todo='presenterm ~/.config/TODO.md'
alias todoedit='nvim ~/.config/TODO.md'This block contains aliases that run specific programs with specific arguments. The first 3 should be self explanatory with the inline comments.
alias todo='presenterm ~/.config/TODO.md' runs the program presenterm passing the file TODO.md to the program. So by simply typing todo in my terminal, my "TODO" list pops up in my terminal as a presenterm tui presentation. And by simply typing todoedit in my terminal, my "TODO" list is opened in Neovim to edit. By the way, if you have not checked out presenterm, it is an awesome markdown terminal presentation (slide show) program. presenterm website
# Change directory aliases
alias cd~='cd ~'
alias cd..='cd ..'
alias ..='cd ..'This block contains aliases to be lazy... No need to put a space between cd and ~ or ... Also just double tap the period to go up a folder level.
# Automatically do an ls after cd
cd() {
if [ -n "$1" ]; then
builtin cd "$@" && ls
else
builtin cd ~ && ls
fi
}This block is actually a bash "Function" vs an "Alias". It is like a mini bash program made available within the bash session. Without getting into bash scripting, this function overrides the built in cd command by actually running the built in cd and tacks on an ls command. This is useful since most of the time that anyone navigates to a folder with cd, they immediately do an ls to view the folder contents. This could be done as an alias but this does one more thing. It checks if you typed anything after cd and if you did it does the above, but if you only typed cd it adds ~ which takes you to home folder (and does an ls).
... more to come
