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.

  1. Spaces: Paths with spaces need to be quoted or escaped.

  2. Asterisks (*): Must be quoted if intended as a literal; otherwise, they will be interpreted as wildcards.

  3. Commas (,): Rarely cause issues, but in some contexts, quoting them is safer.

  4. Shell Special Characters: Characters like ~, &, |, <, >, etc., should be quoted or escaped to prevent the shell from misinterpreting them.

  5. 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 \ .

      --classpath="~/My Project/classes:/path/with spaces/file.jar"
      --classpath=~/My\ Project/classes:/path/with\ spaces/file.jar
  • Windows:

    • Wrap the path in double quotes to handle spaces.

      --classpath="C:\My Files\classes;C:\Program Files\path with spaces\file.jar"

2. Asterisks (*)

  • macOS/Linux:

    • To prevent the shell from expanding the * as a wildcard, use quotes or escape it with \.

      --classpath="/path/with/*wildcards"
      --classpath=/path/with/\*wildcards
  • Windows:

    • As Wildcards: No need for quotes; cmd interprets them as wildcards.

      As Literals: Use double quotes to avoid wildcard interpretation.

      --classpath="C:\path\with\*wildcards"

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.

      --classpath="~/path,with,commas/classes"
  • Windows:

    • Generally, commas are not special in cmd, but wrapping the path in double quotes ensures safety.

      --classpath="C:\path,with,commas\classes;C:\another,path\example.jar"

4. $, &, <, >, ` (Backtick)

  • macOS/Linux:

    • Escape with \ or wrap the argument in single quotes (') to treat them as literals.

      --classpath=/path/with/\$dollar\&symbol\file.jar
      --classpath='/path/with/$dollar&symbolfile.jar'
  • Windows:

    • Escape these characters with the caret (^) or wrap the entire path in double quotes.

      --classpath=C:\path\with^&special\$characters^file.jar
      --classpath="C:\path\with&special\$charactersfile.jar"

5. Tilde (~)

  • macOS/Linux:

    • The tilde is expanded to the user’s home directory. Escape with \ or wrap in quotes to prevent expansion.

      --classpath="\~/MyApp/classes:/path"
  • 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.

      --classpath="C:\Users\~username\path\classes"

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

      --classpath="~/My Files/classes:/path/with\$special\&characters\`file.jar:/path/with/*wildcards"
    • Windows Example

      --classpath="C:\My Files\classes;C:\path\with^&special\$characters^`file.jar;C:\path\with\*wildcards"

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?