Files
homelab/active/software_bash/bash.md
ducoterra 7b93f740ec
Some checks failed
Reese's Arch Toolbox / build-and-push-arch-toolbox (push) Failing after 2m40s
Podman DDNS Image / build-and-push-ddns (push) Failing after 5s
wyoming updates, borg manual, fedora kscreen, bambu wifi, vscode pylance, stable diffusion
2025-05-23 13:49:03 -04:00

101 lines
2.0 KiB
Markdown

# Bash Tips
- [Bash Tips](#bash-tips)
- [bashrc](#bashrc)
- [Eternal History](#eternal-history)
- [Global bashrc](#global-bashrc)
- [User Environment](#user-environment)
- [User Aliases](#user-aliases)
- [Check if executable exists](#check-if-executable-exists)
- [Check if file exists](#check-if-file-exists)
- [Check if last command exit code was zero](#check-if-last-command-exit-code-was-zero)
## bashrc
### Eternal History
```bash
# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=-1
export HISTSIZE=-1
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
```
### Global bashrc
```bash
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
```
### User Environment
```bash
# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]; then
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH
```
### User Aliases
Allows custom aliases in ~/.bashrc.d
```bash
# User specific aliases and functions
if [ -d ~/.bashrc.d ]; then
for rc in ~/.bashrc.d/*; do
if [ -f "$rc" ]; then
. "$rc"
fi
done
fi
unset rc
```
## Check if executable exists
```bash
if command -v aws 2>&1 >/dev/null
then
echo "aws command found"
else
echo "aws could not be found"
fi
```
## Check if file exists
```bash
FILE=/etc/resolv.conf
if [ -f "$FILE" ]; then
echo "$FILE exists."
else
echo "$FILE does not exist."
fi
```
## Check if last command exit code was zero
```bash
cat /i/do/not/exit
if [ $? -eq 0 ]
then
echo "Success."
else
echo "Failure." >&2
fi
```