Skip to content

Shell Quoting, Globbing, and Safe Command Arguments: A Beginner Guide

Admin
October 19, 2026
12 min read
Beginner
πŸ’» Target OS: Ubuntu 24.04+ / Debian 12+ / RHEL 9+

Before you begin

  • Applies to:Linux with Bash 4 or later and GNU coreutils. The examples use Bash-specific syntax.
  • Where to start:Open Bash with a regular account. Run the commands in order in the temporary folder created by the guide.
  • What you need:Have a writable temporary directory. The example paths and wildcard output order can vary.
Guide tools
Guide tools
0/0 Steps (0%)

Spaces, quotes and wildcard characters affect the arguments a command receives. This guide makes those arguments visible before using them with real files. It uses Bash 4 or later and GNU coreutils on Linux; Bash arrays and the helper function are not portable POSIX sh syntax.

Run the steps in order as a normal user. All created files stay inside one temporary directory. Outputs are representative; directory paths and wildcard ordering can vary.

Bash expands command words, then passes separate arguments to a command; quoting controls splitting and wildcard expansion.

01

Open a Clean Bash Practice Shell

Start Here

Start a separate Bash without personal startup files. Run the following inspection commands in the new shell. Set the wildcard options used by this exercise explicitly; the changes end when you leave this shell.

Terminal window
bash --noprofile --norc
Terminal window
printf 'Bash: %s\n' "$BASH_VERSION"
shopt -u nullglob failglob dotglob
02

Create Files with Deliberately Tricky Names

Practice

Save your starting directory, create a unique lab, then enter it. The filenames include spaces, a leading dash and a hidden file. Keep this shell open so its variables remain available through cleanup.

Terminal window
CC_QUOTE_START=$PWD
CC_QUOTE_LAB=$(mktemp -d "${TMPDIR:-/tmp}/cc-quotes.XXXXXXXX") || exit 1
cd -- "$CC_QUOTE_LAB" || exit 1
touch -- 'report one.txt' 'report two.txt' notes.md .hidden.txt ./-draft.txt
pwd
ls -la
03

Print Each Argument Separately

Practice

Define a helper that reports the number of arguments and prints each one in brackets. Using a fixed printf format ensures the input is treated as data. This previews shell argument handling without modifying the matching files.

Terminal window
cc_show_args() {
local item
printf 'argument count: %s\n' "$#"
for item in "$@"; do
printf '<%s>\n' "$item"
done
}
cc_show_args 'report one.txt' 'report two.txt'
❯ Illustrative Output (Results Vary)
argument count: 2
<report one.txt>
<report two.txt>
04

Compare Single Quotes and Double Quotes

Practice

Single quotes keep the characters inside literal. Double quotes allow variable expansion while keeping the expanded value together as one argument. The quote characters themselves are removed before the function receives its arguments.

Terminal window
CC_REPORT='report one.txt'
cc_show_args '$CC_REPORT'
cc_show_args "$CC_REPORT"
❯ Illustrative Output (Results Vary)
argument count: 1
<$CC_REPORT>
argument count: 1
<report one.txt>
05

See Why an Unquoted Variable Can Split

Practice

The first call intentionally omits quotes to demonstrate splitting with the normal whitespace field separators. It is safe here because the helper only prints arguments. The second call preserves the entire filename. The ShellCheck suppression belongs only to the deliberately unsafe demonstration line.

Terminal window
# shellcheck disable=SC2086
cc_show_args $CC_REPORT
cc_show_args "$CC_REPORT"
❯ Illustrative Output (Results Vary)
argument count: 2
<report>
<one.txt>
argument count: 1
<report one.txt>
06

Compare a Wildcard with a Literal Pattern

Practice

An unquoted pattern matches filenames in the current directory. Quoting the pattern passes the literal characters instead. The plain star does not select the hidden file with the options chosen in Step 1. A wildcard match containing a space still forms one argument per matched pathname.

Terminal window
cc_show_args ./*.txt
cc_show_args './*.txt'
cc_show_args ./*.no-such-extension
❯ Illustrative Output (Results Vary)
The first call has three arguments: ./-draft.txt and the two report files.
The second call has one argument: ./*.txt
With the selected options, the unmatched pattern remains literal.
07

Store a Reviewed File List in an Array

Practice

Use a Bash array when one variable must retain several filenames. Quoted at-sign expansion produces one argument for each array item. This lab guarantees matches; an automation script must also handle the no-match case and decide how hidden files should be treated.

Terminal window
CC_REPORTS=(./report*.txt)
cc_show_args "${CC_REPORTS[@]}"
for item in "${CC_REPORTS[@]}"; do
printf 'Would process: <%s>\n' "$item"
done
08

Handle a Filename That Starts with a Dash

Practice

GNU tools commonly accept a double dash to end option parsing. A dot-slash prefix also makes a relative pathname start with a dot rather than a dash. Quoting protects spaces but does not by itself stop a leading dash being interpreted as an option.

Terminal window
ls -l -- '-draft.txt'
ls -l ./-draft.txt
09

Copy a Space-Containing Filename Safely

Practice

Preview the exact source and destination, then copy one file inside the lab. The destination is new and unique to this exercise. GNU cp options and the double dash belong to the command, while quoted paths identify the operands. The cmp exit status should be zero for identical files.

Terminal window
cc_show_args "$CC_REPORT" 'report copy.txt'
cp -- "$CC_REPORT" 'report copy.txt'
if cmp -s -- "$CC_REPORT" 'report copy.txt'; then
printf 'Copy matches the source\n'
else
printf 'Comparison failed; inspect both files\n' >&2
fi
10

Remove Only the Lab Files

Cleanup

Return to your starting directory first. Remove the exact files created by the exercise, then remove the empty lab directory. An unexpected remaining file causes rmdir to fail rather than being recursively deleted. Finish with exit to return to the original shell.

Terminal window
cd -- "${CC_QUOTE_START:?Run the setup step first}" || exit 1
: "${CC_QUOTE_LAB:?Lab directory is not set}"
rm -- "$CC_QUOTE_LAB/report one.txt" "$CC_QUOTE_LAB/report two.txt" \
"$CC_QUOTE_LAB/notes.md" "$CC_QUOTE_LAB/.hidden.txt" \
"$CC_QUOTE_LAB/-draft.txt" "$CC_QUOTE_LAB/report copy.txt"
rmdir -- "$CC_QUOTE_LAB"
unset CC_QUOTE_START CC_QUOTE_LAB CC_REPORT CC_REPORTS
unset -f cc_show_args

References: GNU Bash quoting and shell expansion. Continue with pipes and redirection or safe batch file processing.

Comments