On a Mac, put a wildcard pattern in single quotes to stop the default zsh shell from expanding it before the command runs. For example, printf '%s\n' '*.csv' passes the literal text *.csv as one argument. The quotes are removed by the shell; the asterisk reaches the program. If the program uses patterns itself, it may interpret that asterisk afterward.

Why the wildcard changes before the command runs

When you enter a command in Terminal, the shell processes its arguments before starting the program. In zsh, an unquoted * can trigger filename generation: the shell substitutes matching filenames for the pattern. If there are no matches, zsh normally reports an error, though shell options can change that behavior. The zsh manual describes filename generation and its no-match behavior.

Apple identifies zsh as the default shell in Terminal, although a Mac can be configured to use another shell. Apple’s Terminal guide explains the default and how to change it. The single-quote method below is suitable for zsh and Bash; if you use a different shell, check its manual because quoting rules can vary.

Use single quotes for a literal wildcard

Type the wildcard pattern between single quote marks, preserving any other characters that belong to it. For example:

printf '%s\n' '*.csv'

The output is the literal *.csv, whether or not the current directory contains CSV files. printf receives the pattern as an argument and prints it; the shell does not expand the asterisk. The quotes are shell syntax, so they are not included in the argument. Apple’s shell scripting guide explains that single quotes disable shell expansion within the quoted text. Apple: Flow Control, Expansion, and Parsing.

This is especially important when a program expects to handle a pattern itself. On macOS, for example, find can match file names using the pattern supplied to -name:

find . -type f -name '*.log'

Here, the shell passes *.log literally to find; then find applies its own filename matching to files beneath the current directory. Quoting prevents the shell from replacing the pattern with names in the current directory before find can use it. The pattern’s meaning depends on the receiving program: shell quoting controls shell expansion, not how the program interprets its argument.

When to use a backslash instead

For a short pattern, a backslash can protect an individual wildcard outside quotes:

printf '%s\n' \*.csv

This also passes the asterisk literally. Single quotes are often easier to read when a pattern contains several special characters, spaces, or brackets. For instance, 'chapter [0-9]*.txt' keeps the brackets and wildcard from being interpreted by the shell. Apple documents backslash quoting and single-quote behavior in its shell scripting guide.

Common quoting mistakes

Double quotes do not reliably protect every shell character. They prevent wildcard expansion in zsh, but still allow certain expansions, including variables such as $name and command substitutions such as $(command). For a pattern that should arrive exactly as typed, use single quotes unless the argument needs one of those expansions.

A single quote cannot appear inside a single-quoted string as an ordinary character. If the pattern itself contains an apostrophe, close the quoted part, add the apostrophe using another quoting method, and reopen the single quotes; Apple shows this shell quoting technique in its guide. For unusual patterns, inspect the exact command carefully before running it.

Also distinguish shell expansion from the receiving program’s behavior. Quoting *.log does not force a program to treat the asterisk as an ordinary character; it only ensures the shell passes it through. A utility such as find -name may interpret it as a pattern, while another command may treat it literally. Check that command’s own documentation when the distinction matters.

A quick check before pressing Return

Ask what the program should receive. If it needs a wildcard pattern, place the pattern in single quotes and confirm that the command’s options are set to interpret patterns as intended. If the program should receive an actual filename, leave shell expansion enabled where appropriate, or provide the filename directly. The key is to quote at the shell boundary when you want the program to receive the wildcard characters themselves.

Sources