beginner
Good starting points for newcomers
-
Script arguments
How a script reads what it was called with: positional parameters, why "$@" and "$*" are not the same, shift, and parsing real flags with getopts.
-
Functions
Defining bash functions, why return gives a status not a value, how to hand data back, and why local separates a helper from a landmine.
-
Arrays
Indexed and associative arrays in bash: building them, why the @ and * subscripts differ, and filling one from command output without splitting.
-
Parameter expansion
Bash parameter expansion: default values with :-, trimming paths with # and %, replacing text with //, and slicing, length and case conversion.
-
Where a script lives
What $0 really contains, why a script cannot find a file next to itself with a relative path, and the BASH_SOURCE line that works through a symlink.
-
Arithmetic
Integer arithmetic in bash with $(( )) and (( )), -eq against =, why a leading zero is an error, and the awk one-liner for sums bash cannot do.
-
Here-docs and generating files
Here-documents in bash: feeding a block of text to a command, quoting the terminator to stop expansion, generating config files, and here-strings.
-
Traps and cleanup
The mktemp and trap EXIT pattern for scripts that clean up after themselves, why the trap body needs single quotes, and what set -e does not cover.
-
Debugging and robustness
Reading what a script actually did with bash -x and PS4, checking syntax without running it, making unset variables fatal, and what shellcheck catches.
-
A real script
A capstone backup script using arguments, arrays, parameter expansion, a trap and real exit codes, and how to schedule it with cron or a systemd timer.
-
Add a directory to your PATH permanently
How to put a directory on PATH for good on Debian, why ~/.local/bin needs no configuration at all, and what to do for any other directory.
-
Save everything a terminal session prints
Keep a copy of what a command printed with tee, or record an entire terminal session with script, including the exit status trap that catches people out.
-
command not found
The shell says this for a command that is missing, one that is not on PATH, one it remembers in the wrong place, and a script whose interpreter is gone.
-
Could not get lock /var/lib/dpkg/lock-frontend
apt refuses to run because something else is already installing. Modern apt tells you exactly which process, and deleting the lock file is the wrong fix.
-
The following packages have been kept back
apt-get upgrade refuses to install new packages, so an upgrade that needs one is held back instead. What the message means and the three things that cause it.
-
sudo: command not found
On Debian, sudo is often simply not installed, and your user is not in the sudo group. Why the installer does that, and how to fix it from a root shell.
-
apt vs apt-get
The same package ships both, and they do the same job. What differs is who each is written for, and only one of them is safe in a script.
-
Hard links vs symbolic links
A hard link is another name for the same data, a symlink a small file holding a path. What that means when the original goes, when sed edits it, and on disk.
-
remove vs purge vs autoremove
Not three strengths of one operation. Two of them differ over configuration files; the third acts only on packages you never asked for.
-
sh vs bash vs dash
On Debian /bin/sh is dash, not bash. That one fact explains most scripts that work at your prompt and fail the moment something else runs them.
-
which vs type vs command -v
which searches PATH and nothing else, so it reports that cd does not exist. What type and command -v answer instead, and which one belongs in a script.
-
Which package provides a file
Trace a file to the package that ships it: dpkg -S for what is installed, apt-file for what is not, and the files that belong to no package at all.