Search⌘ K

Poor Technical Solution

Explore the drawbacks of relying on the Bash history file to store complex backup commands, including size limits and search inefficiencies. Learn why permanent storage needs require more robust solutions and how Bash scripting features can address these challenges.

Saving our commands

Our following backup command became long and complex after applying all improvements:

(tar -cjf ~/photo.tar.bz2 ~/photo &&
  echo "tar - OK" > results.txt ||
  ! echo "tar - FAILS" > results.txt) &&
(cp -f ~/photo.tar.bz2 ~/backup &&
  echo "cp - OK" >> results.txt ||
  ! echo "cp - FAILS" >> results.txt)

Therefore, we should store it somewhere. Otherwise, we have to type the command in the terminal window each time. Typing is ultimately a bad idea because we can make a mistake or forget something.

Bash has an option to store frequently-used commands. The history file saves everything we executed in the terminal. The file is unique for each user and has the ~/.bash_history path. When we press the “Ctrl+R” (or “control+R” in macOS) keystroke in the terminal window, Bash calls the quick search over the history. We can ...