Bash Aliases, Functions, and Environment Variables
I customized the shell with aliases, small functions, and exported environment variables to cut keystrokes and standardize tool behaviour across hosts. Centralizing PATH and tool defaults in one sourced file gave every session a consistent, productive baseline.
Objective & Context
The environment shapes how every command runs. This lab covers aliases for shorthand, functions for parameterized shortcuts, and exported variables (PATH, EDITOR, history settings), the personalization layer the startup-files lab makes persistent.
Environment & Prerequisites
- A Bash shell with a writable home directory.
- Understanding of shell vs environment variables.
- A tool you invoke often to alias.
flowchart LR
V[shell var x=1] -->|export| E[env var]
E --> C[child processes inherit]
V -.not inherited.- C
Step-by-Step Execution
1. Define aliases and a function
alias ll='ls -lAh'; gco() { git checkout "$1"; }2. Export environment variables
export EDITOR=vim; export PATH="$HOME/.local/bin:$PATH"3. Verify inheritance in a child shell
bash -c 'echo $EDITOR'vim
Validation & Testing
Confirm an alias works in the current shell and that an exported variable is visible to a child process while an unexported one is not. Pass criteria: aliases expand, exported variables inherit, and PATH changes take effect for new commands.
Advanced: Troubleshooting
- Alias missing in scripts: aliases are not expanded in non-interactive scripts; use functions.
- Var not inherited: only
exported variables pass to child processes. - PATH ordering: earlier entries win; prepend to override system binaries.
Key Results
- Reduced common commands to short, memorable aliases.
- Standardized EDITOR and PATH across all sessions.
- Replaced fragile aliases with functions where parameters were needed.
- Verified export semantics with a child-shell test.