Bash: quoting and expansions
The order of shell expansions, the three kinds of quoting, word splitting, the ${v:-d}, ${v%m} and ${v##m} forms and the here-document: predicting the arguments a command receives.
- 28 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Expansion: what is it?
Show the answer
A transformation the shell applies to the command line before starting the program: braces, tilde, variables, commands, arithmetic, word splitting, wildcards.
The program never sees
*or$HOME: it receives the result. It is notlsthat replaces*.txt, it is the shell, before callingls.What does this script print?
```bash
f="mon rapport.txt"
set -- $f "$f"
echo $#`3214
Show the answer
3$funquoted is split into two words (monandrapport.txt),"$f"makes just one: 2 + 1 = 3 arguments.Among the expansions, the shell handles the … first, then the tilde and variables, next …, and finally the wildcards.
Show the answer
Among the expansions, the shell handles the braces first, then the tilde and variables, next word splitting, and finally the wildcards.
echo {a,b}{1,2}givesa1 a2 b1 b2: braces are expanded before everything else, even before variables. Wildcards come last, on the words already split.Quoting: what is it?
Show the answer
A mark that strips a character of its special meaning for the shell: single quotes, double quotes or a backslash.
Single quotes prevent every expansion; double quotes let
$and\through; the backslash protects only the next character.A cleanup script runs
rm $fichierwithfichier="mon fichier"and printsrm: cannot remove 'mon': No such file or directorythenrm: cannot remove 'fichier': No such file or directory. Which fix is right?rm '$fichier'rm -f $fichierrm "$fichier"rm ${fichier}
Show the answer
rm "$fichier"Double quotes keep the variable's content as a single argument. Single quotes would prevent expansion (
rmwould receive the text$fichier),-fonly silences the errors, and braces protect nothing from splitting.