Understanding the Bash Shell

Oct 31, 2025

Redirection and Piping

Redirect STDOUT to file

command > file

Redirect STDOUT to file and append

command >> file

Redirect STDERR

command 2> error.log

Redirect STDOUT and STDERR to separate files

command > output.log 2> error.log

Redirect STDERR to STDOUT and then to /dev/null

command &> /dev/null # non-POSIX conforming, bash only
command > /dev/null 2>&1 # more portable

Pipe STDOUT of one command to STDIN of another

command | another-command

Shell History

Print history

history

Repeat a command from history

!{line-number}

Commit session history

history -w

Remove a specific command from history

history -d {line number}

Clear history

history -c

Search backward in history Ctrl + r

Search with partial matches

echo '"\e[A: history-search-backward"' >> ~/.inputrc
echo '"\e[B: history-search-forward"' >> ~/.inputrc

Terminal Keyboard Shortcuts

Go to beginning of line Ctrl-a

Go to end of line Ctrl-e

Clear current line Ctrl-u

Clear terminal window Ctrl-l

Shell Expansion

Globbing

All characters *

ls *

Single character ?

ls foo-?.txt

Ranges

ls [a-z]*
  • [a-z] = all lowercase characters of the alphabet
  • [A-Z] = all uppercase characters of the alphabet
  • [a-zA-Z] = all characters of the alphabet, irrespective of their case
  • [j-p] = lowercase characters j, k, l, m, n, o or p
  • [a-z3-6] = lowercase characters or the numbers 3, 4, 5 or 6

Brace expansion

touch file{1..9}

Tuning Bash Environment

  • Global profile configurations in /etc/profile and /etc/profile.d
  • User specific profile configurations in ~/.bash_profile
  • Global bash configurations in /etc/bashrc
  • User specific bash configurations in ~/.bashrc

The difference between these is profile configurations are sourced by login shells and bashrc configurations are sourced by shells opened after logging in. Configurations in bashrc should be specific to the interactive shell environment like aliases, functions, prompt, etc. Configurations that should be available to all process started during a login session should be in profile.

Lab Exercise

  1. If a user requires a variable to be defined once every time they login, where should that variable be defined?
  2. If a user requires an alias to be present in every shell, where should that alias be defined?
  3. If a user requires a function to be present in every shell, where should that function be defined?
  4. If the history file needs to grow to 2500 entries for all users, what variable needs to be defined and where?
  5. If the history file needs to grow to 10000 entries for a specific user, what variable needs to be defined and where?
  6. If the in-memory history needs to be limited to 250 entries for a specific user, what variable needs to be defined and where?
Solution
  1. ~/.bash_profile is sourced once per login
    • echo "export varName=varValue" >> .bash_profile
  2. ~/.bashrc
  3. ~/.bashrc
  4. $HISTFILESIZE should be modified in a drop-in file in /etc/profile.d
  5. $HISTFILESIZE in the user’s ~/.bash_profile
  6. $HISTSIZE in the user’s ~/.bash_profile

- Clint Jordan