Linux

Bash: Variables, Expansion & Globbing

43 flashcards · answers and spaced-repetition review in the KnowCard app

Before running a destructive command like rm on a glob, how can you preview exactly which files the pattern will hit?
By default echo * omits hidden files — how do you make globbing include dotfiles, and how do you turn it back off?
Why does find ... | ls -l show the current directory instead of find's results, and what two mechanisms actually pass find's output to ls?
When feeding a very large list of files to a command, why can xargs succeed where $(command) substitution fails?
Piping filenames through find | xargs breaks when a filename contains a space — what pair of options makes it robust?
A beginner writes var = abc to set a variable and it fails — what is the exact syntax rule being violated?
When you want a variable's value immediately followed by more text, why can echo $axxx fail and what notation fixes it?
How do you capture a command's output into a variable, and why is that value gone after you close the terminal?
You set a=3 in your shell, then start a script — the script can't see a. What rule explains this, and how do you make the value visible to child processes?
You want to see only the environment variables (the ones children inherit), not every shell variable — which command, and what does set show instead?
bash predefines PATH as a colon-separated directory list it searches for commands — where should you edit it for all users versus just yourself?
Match each predefined bash variable to what it holds: HOME, OLDPWD, PWD, LOGNAME, HOSTNAME, BASH.
What is the practical difference between the PS1 and PS2 prompt variables, and what does RANDOM give you?
When you run a command like cat *.txt, which component actually turns *.txt into the list of matching filenames, and when does that happen?
In a glob pattern, what is the difference in what a single ? matches versus a single *, and which files does * quietly skip?
How does bracket globbing differ between [a-f], [abc], and [!abc], and what does the ^ form do?
You want to create directories dir1, dir2, dir3 that don't exist yet — why does mkdir dir* fail but brace expansion work?
What does the brace-expansion sequence form {a..b} produce, and how does {z..t} behave?
Someone runs ls * in a directory with only a handful of entries and gets a huge listing — why, and which option fixes it?
What kind of data can a plain bash variable hold, and what is the asymmetry between assigning it and reading it?
You want the double-star pattern to find PDFs recursively through the current directory and every subdirectory, but it just behaves like an ordinary single star — what's missing?
Both $(command) and backtick command notation run a command and insert its output — why is $(...) the recommended form?
Some programs pop open vi to edit a file, but you prefer a different editor — which variables control that choice and where do you set them?
Which glob trick lists only the directories in the current folder, and why does it work?
You run ls -R *.tex to list .tex files in all subdirectories but it doesn't recurse — why not?
Why can't you rename every .x file to .y in one shot with mv *.x *.y?
Why is .* a dangerous pattern for matching hidden files, and what pattern matches them safely?
Both single and double quotes group a string, but echo "$PATH" prints your path while echo '$PATH' prints the literal text — why?
You need to delete a file literally named ab* $cd — how do single quotes make the shell treat every special character as literal?
How do you do arithmetic in bash such as adding 2 and 3, and what surprising limit does the arithmetic have?
Profile files set environment variables for your shells — what file sets them in a way that isn't tied to the shell's startup files?
How do you add ~/bin to PATH so your own scripts run without typing their full path, without wiping the existing PATH?
How do you define an indexed array in bash, and how do you read one element versus all of them?
You try to use string keys in a bash array but every key lands in element 0 — what declaration did you forget?
What is the quickest way to load every line of a text file into successive elements of a bash array?
In parameter substitution, what's the difference between ${var:-default} and ${var:=default}?
Which parameter-substitution forms yield a value only when a variable IS set, and which aborts the script when it is empty?
How do you get a bash variable's length, and how do you slice out a substring by offset?
Given fn=book.tar.gz, which ${...} operators strip from the front versus the back of the value, and how do single versus doubled symbols differ?
How do you do search-and-replace inside a bash variable, and how do you replace every occurrence instead of just the first?
A variable holds the NAME of another variable — how do you read through to the second variable's value in one step?
In a for-loop range like {1..12}, how do you get leading zeros (01, 02, …) or a step other than 1?
Can a single glob pattern reach into subdirectories? What do */*.jpg and /usr/*bin/* each match?

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play
Bash: Variables, Expansion & Globbing (Linux) · KnowCard