Quoting command line arguments
When dealing with command line arguments such as the --classpath
option in different operating systems, proper use of quotes ensures that arguments containing special characters, spaces, or symbols are correctly interpreted. There are various special characters that require special care when used in arguments.
Spaces: Paths with spaces need to be quoted or escaped.
Asterisks (
*
): Must be quoted if intended as a literal; otherwise, they will be interpreted as wildcards.Commas (
,
): Rarely cause issues, but in some contexts, quoting them is safer.Shell Special Characters: Characters like
~
,&
,|
,<
,>
, etc., should be quoted or escaped to prevent the shell from misinterpreting them.Dollar Signs (
$
): Require escaping or quoting, as$
is used for variable interpolation.
Special Characters and Escaping
Each operating system has slightly different conventions for handling arguments with such characters.
1. Spaces
macOS/Linux:
Wrap the entire string in double quotes or escape spaces with
\
.
Windows:
Wrap the path in double quotes to handle spaces.
2. Asterisks (*
)
macOS/Linux:
To prevent the shell from expanding the
*
as a wildcard, use quotes or escape it with\
.
Windows:
As Wildcards: No need for quotes; cmd interprets them as wildcards.
As Literals: Use double quotes to avoid wildcard interpretation.
3. Commas (,
)
macOS/Linux:
Commas don’t need escaping unless used in contexts where they are interpreted by the shell or program. Quotes are safer in those cases.
Windows:
Generally, commas are not special in cmd, but wrapping the path in double quotes ensures safety.
4. $
, &
, <
, >
, `
(Backtick)
macOS/Linux:
Escape with
\
or wrap the argument in single quotes ('
) to treat them as literals.
Windows:
Escape these characters with the caret (
^
) or wrap the entire path in double quotes.
5. Tilde (~
)
macOS/Linux:
The tilde is expanded to the user’s home directory. Escape with
\
or wrap in quotes to prevent expansion.
Windows:
The tilde is not expanded as the home directory shortcut in cmd. However, it can be used as part of the path without escaping. For consistency, wrap in quotes if spaces or other special characters are present.
6. Comparison Between macOS/Linux and Windows cmd
Below is an example comparing how you would handle
--classpath
in macOS/Linux vs. Windows cmd.macOS/Linux Example
Windows Example
Summary of Quoting Rules
Character
Linux/macOS (bash/zsh)
Windows (cmd)
Spaces
Double quotes ("
) or escape (\
)
Double quotes ("
)
Asterisks (*
)
Double quotes or escape (\
)
Double quotes for literals
Commas (,
)
Optional quotes
Optional quotes
$
, &
, <
, >
, `
Double quotes or escape (\
)
Double quotes or escape (^
)
~
Expands to home directory; quote or escape (\
)
No expansion; use quotes for safety
Last updated
Was this helpful?