Linux › Section B · Basic commands and the directory hierarchy › Topic B.6
Special characters and command-line editing
A handful of punctuation marks have special meanings at the command line, and knowing their names and jobs lets you read commands, talk about them and spot mistakes. This topic also covers the shell's brace, tilde and variable expansions and the keystrokes that make editing a command line fast.
Why the punctuation matters
In a command line, many symbols are not plain text: the shell treats them as instructions. A star becomes a list of file names, a dollar sign introduces a variable, and a hash starts a comment. If you know what each symbol does, an unfamiliar command such as cp ~/notes/{a,b}.txt "$DEST" becomes readable rather than mysterious. You also need their names to talk about commands, since people say "pipe it to grep" or "put a tilde in front". The cards below give the common names in use in the United States and the main jobs of each character.
Names and jobs of the common characters
* is a star or asterisk: a glob, and repetition in regular expressions. . is a dot: the current directory and a separator in file and host names. ! is a bang: negation and history recall. | is a pipe. / is a slash: the directory separator. \ is a backslash: it makes the next character literal, never a directory separator. $ is a dollar: variables, and end of line in regular expressions. ' is a single quote or tick, ` a backtick or backquote, and " a double quote. ^ is a caret: start of line and negation. ~ is a tilde. # is a hash: comments. [ ] are square brackets, { } braces, and _ an underscore, used where spaces are unwanted.
Quoting in brief
Quotes control which special characters the shell acts on. Inside single quotes nothing is special: echo '$HOME' prints $HOME exactly. Inside double quotes the shell still replaces variables and command substitutions but does no globbing or word splitting, so "$HOME" becomes your home path as one word. A backslash protects the single character after it: echo \* prints a star rather than a list of files. $(command) is replaced by the command's output: echo "Files here: $(ls | wc -l)" printed Files here: 8 in one test directory. Backticks, `command`, are the older form of the same thing. The topic "Script basics, quoting, and literals" goes deeper.
Brace expansion
Braces generate words before anything else is expanded. A comma list inside braces makes one word per item, with any text outside the braces attached to each: echo backup.{tar,zip} prints backup.tar backup.zip, and echo a{,b} prints a ab, because one item is empty. A range with two dots counts: echo file{1..3}.txt prints file1.txt file2.txt file3.txt, and {a..e} gives a b c d e. Unlike globs, brace expansion does not look at existing files, so it can name files that do not exist yet. Quoting turns it off: echo "{x,y}" printed {x,y}.
Worked example: building a project tree
You want a directory proj with subdirectories src, docs and tests. 1. Write mkdir -p proj/{src,docs,tests}. 2. Brace expansion runs first, turning the single word into three: proj/src, proj/docs and proj/tests. 3. mkdir -p then creates proj along with all three subdirectories, as the topic "Navigating directories and wildcards" explained. 4. Run in bash 5.2, the command printed nothing, and ls proj then listed docs, src and tests, sorted by name. 5. To preview any brace expression safely, put echo in front: echo mkdir -p proj/{src,docs,tests} shows the expanded command without running it. Check: three directories from one short word.
Tilde expansion
A tilde at the start of a word becomes a home directory. Alone or followed by a slash, ~ means your own home, so cd ~/docs works wherever you are and echo ~ prints a path such as /home/ana. Followed by a username, it means that user's home: ~ben/shared names a directory in ben's home. The rule applies only at the start of a word, and quoting prevents it: echo '~' and echo "~" both printed a plain ~ in a test. That is why scripts usually write "$HOME" inside double quotes instead. Tilde expansion is done by the shell, so a program reading a file name from a configuration file may not understand ~ at all.
Variable expansion with braces
The shell replaces $NAME with a variable's value, but it must decide where the name ends. It takes the longest run of letters, digits and underscores, so in $NAME_2026.txt it looks for a variable called NAME_2026. Braces mark the boundary exactly: ${NAME}_2026.txt uses NAME and then adds _2026.txt. The braces also allow modifiers. For example, ${COLOR:-none} gives the value of COLOR, or none if COLOR is unset or empty, without changing COLOR. The topics in the section "Shell scripts" use more of these forms.
Worked example: where does the name end?
Set NAME=report and run each command in bash 5.2. 1. echo $NAME_2026.txt printed only .txt. The shell read the name NAME_2026, which is unset and so expanded to nothing, leaving .txt. 2. echo ${NAME}_2026.txt printed report_2026.txt, because the braces ended the name after NAME. 3. echo "$NAME" '$NAME' printed report $NAME: double quotes allowed the expansion and single quotes blocked it. 4. With COLOR unset, echo "${COLOR:-none}" printed none. Check: no error was reported in step 1, which is why such bugs are easy to miss; previewing with echo catches them.
Editing the command line
Bash's line editor, the readline library, accepts arrow keys, but control keys work in many other Unix programs too and keep your hands on the keyboard. A caret before a letter, as in ^C, is shorthand for Ctrl plus that letter. Ctrl+A and Ctrl+E jump to the start and end of the line; Ctrl+B and Ctrl+F move one character back and forward. Ctrl+P and Ctrl+N step to the previous and next command in your history, and Ctrl+R searches it backwards as you type. Ctrl+W erases the word before the cursor, Ctrl+U erases from the cursor to the start of the line, Ctrl+K erases to the end, and Ctrl+Y pastes back what was erased. Tab completes command and file names.
Common mistakes to avoid
First, putting a variable directly before letters, digits or underscores, as in $NAME_2026, when you meant ${NAME}_2026. Second, writing ~ inside quotes and expecting a home directory. Third, assuming brace expansion matches existing files the way * does. Fourth, adding a space inside braces, as in {a, b}, which stops the expansion. Fifth, starting a word with # in the middle of a command: everything after it on that line is ignored as a comment. Sixth, retyping a long command to fix one character instead of recalling it with Ctrl+P and editing it.
Key takeaways
Special characters such as * . ! | / \ $ ' ` " ^ ~ # [ ] { } and _ each have names and jobs; ^C means Ctrl+C. Single quotes block all expansion, double quotes allow variables and $( ) but not globs, and a backslash protects one character. Brace expansion generates words from lists, {a,b}, and ranges, {1..3}, without checking files. A leading ~ means your home, and ~user another user's. ${NAME} marks where a variable name ends and allows forms such as ${VAR:-default}. Readline keys: Ctrl+A and Ctrl+E for line ends, Ctrl+P, Ctrl+N and Ctrl+R for history, Ctrl+W, Ctrl+U, Ctrl+K and Ctrl+Y to cut and paste, Tab to complete.
Flashcards
Flashcard
Star, dot, bang, pipe
Test your understanding
Question 1 of 5
What does echo log{1..3}.txt print?
Question 2 of 5
With FILE=data set and no variable called FILE_old, what does echo $FILE_old.csv print?
Question 3 of 5
Which command prints the four characters $HOME exactly as typed?
Question 4 of 5
You typed a long command and notice a typo in its first word. Which key jumps straight to the start of the line?
Question 5 of 5
No file called report.pdf exists yet. What does echo report.{pdf,docx} print?
