11

I couldn't find anything but is there any reason for choosing % over $ like in *nix shells?

  • 2
    Not only MS-DOS uses % instead of $, but the syntax is different. In *nix, the dollar sign is a sigil, and the percent sign in MS-DOS is more like a magic quotation mark, as variable names must be enclosed in percent signs to be interpolated, rather than just preceded by a percent sign (e. g. %PATH%). This avoids the need to have another magic character, like parentheses to indicate the variable name boundary, like in $(foo)bar. Then it makes sense to use a new character to indicate its new semantics. – Leo B. Aug 14 '18 at 2:07
  • 3
    @LeoB. Nitpick: In Bash, $(foo) means "run command foo and insert its stdoutput into the command whereas (in POSIX shell also) ${foo} means "value of variable $foo. – wizzwizz4 Aug 14 '18 at 18:48
  • @wizzwizz4 I was thinking about the Makefile syntax where the parentheses are mandatory. – Leo B. Aug 14 '18 at 19:16
6

By using a \ as path separator, they needed a different marker for control character encoding, and did choose $ which in turn meant that the variable marker need to be another one, which turned out as %.

So by choosing % instead they could have kept $ for variables, but then again is the parser structured a bit different as it allows concatenated strings, thus needing a finishing marker at the end of named variables. So they didn't and the rest is history :))


I'm not complete so sure right now about the full implications, but $ is used at least in three different ways on the MS-DOS command line and in batch.

  • Expand a batch variable to a full path name with %~$PATH:<varnumber>
  • Defining special content for the PROMPT command like date and time as in prompt $d $t$g.
  • Code the escape character in parameters as $e like echo $e[0m for screen reset.

Especially the does give the lead here, as marking special characters in Unix is done via a backslash \, which in DOS marks a directory level. MS had to use $ instead, so \e became $e.

Further, but way later (DOS 5)

  • DOSKEY used $ to indicate special functions in macro generation.

Surprisingly, its predecessor CP/M did use $ as variable indicator as well as marker for special meanings in some commands (like privileges in STAT).

In CP/M batch files are run with the SUBMIT command, which will read a file (standard ending .SUB) and feed each line to the OS, after replacing variables marked with $. DOS has this feature build in and treats .BAT files like programs.

  • 2
    In bash they resolve the ambiguities by adding brackets ${var} – phuclv Aug 14 '18 at 0:55
  • CP/M's CCP doesn't give $ any special meaning. STAT and SUBMIT are transients (separate programs) that interpret command-line parameters or strings in the input themselves. – Blrfl Aug 14 '18 at 13:28
  • 4
    @phuclv Remember that bash is a Johnny-come-lately; that strategy is of a far more ancient lineage. – tchrist Aug 14 '18 at 13:28
  • / was used for options in existing programs so `` was most likely chosen to avoid breaking those. – Thorbjørn Ravn Andersen Sep 29 '18 at 23:05
0

I can't know for sure but I suspect one of the reasons was that $ was used as the string terminating character in the standard DOS "print string" API (int 21h ah=9h), a convention apparently inherited from CP/M.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.