RH124 Ch 2: Working in the Bash Shell

The shell reads a line, expands it, then runs the command. Knowing what the shell does before the command runs explains most surprises.

Expansion - Globs: (anything), ? (one character), [abc] (one of these). ls /etc/.conf - Braces: touch report{jan,feb,mar}.txt makes three files; {1..5} makes a range. - Tilde: ~ is your home directory, ~ravi is ravi's. - Variables and commands: echo $HOME, echo "Today is $(date +%A)". - Quoting: single quotes stop all expansion, double quotes allow $ and $(...), backslash escapes one character.

Redirection — every process has stdin (0), stdout (1) and stderr (2). - cmd file replaces the file; cmd file appends. - cmd 2 errors.txt captures only errors; cmd 2 /dev/null throws them away. - cmd all.txt 2&1 (or & all.txt) sends both to one file. Order matters: 2&1 file does not do the same thing.

Pipes join commands: grep bash /etc/passwd | wc -l. tee saves a copy while passing it on: ls -l | tee list.txt | less.

History and editing: history, !42 reruns command 42, Ctrl+R searches backwards, Tab completes names, Ctrl+A/Ctrl+E jump to the start/end of the line.

Common mistakes - Using when you meant — the file is emptied before the command even runs. - Forgetting that globs are expanded by the shell, so ls .conf in an empty directory passes the literal .conf. - Redirecting a root-owned file with sudo echo x /etc/file — the redirection runs as you, not root. Use echo x | sudo tee /etc/file.