cmd.rst
author Oleksandr Gavenko <gavenkoa@gmail.com>
Tue, 07 Feb 2023 00:53:39 +0200
changeset 2555 70383fa8bf12
parent 2540 d699ee7377a4
permissions -rw-r--r--
About copying SSH pubkey.

.. -*- coding: utf-8; -*-

====================
 CMD Windows shell.
====================
.. contents::
   :local:

Defining prompt
===============

Set the environmet variable ``PROMPT``, example of ``.reg`` file::

  [HKEY_CURRENT_USER\Environment]
  "PROMPT"="cmd# "

I embedded current time and directory; also a new line for readability of commands when a current
working directory path is too long::

  [HKEY_CURRENT_USER\Environment]
  "PROMPT"="$T$S$P$_cmd#$S"

CMD has built-in command to modify this env var::

  prompt "FORMAT"

To list available options use ``prompt /?``.

Resize a conhost window
=======================
::

  cmd# mode CON: COLS=120 LINES=40

Quoting.
========

* Arguments are delimited by white space, which is either a space or a tab.
* A string surrounded by double quotation marks is interpreted as a single
  argument.
* A double quotation mark preceded by a backslash, \", is interpreted as a
  literal double quotation mark.
* Backslashes are interpreted literally, unless they immediately precede a
  double quotation mark.
* If an even number of backslashes is followed by a double quotation mark,
  then one backslash (\) is placed in the argv array for every pair of
  backslashes (\\), and the double quotation mark (") is interpreted as a
  string delimiter.
* If an odd number of backslashes is followed by a double quotation mark,
  then one backslash (\) is placed in the argv array for every pair of
  backslashes (\\) and the double quotation mark is interpreted as an escape
  sequence by the remaining backslash, causing a literal double quotation
  mark (") to be placed in argv.
* In double quote mark need surround such chars::

    & < > [ ] { } ^ = ; ! ' + , ` ~ %

  Also all this char can be escaped by ^ char.
* Long line can be truncated by ^ char, in this case trailing white spaces
  not allowed.
* To quote percent sign % before alpha char in batch file double it
  occurrences or plase in quotes::

    prog '%'HOME'%' "%"HOME"%" %%HOME%

http://msdn.microsoft.com/en-us/library/ms880421.aspx
  Parsing C Command-Line Arguments

Variables
=========

Variable name start with letter and underscore, next chars can be letter,
number and underscore. Variable name is case insensitive.

List variables::

  cmd# set
  ...
  VAR=VALUE

Use syntax ``%NAME%`` to expand ``NAME`` value.

Use ``set`` command to define environment variable (``/p`` is with a user input, ``/a`` for
arithmetic expressions)::

  set NAME=VALUE
  set /p PASS="Enter password: "
  set /a NUM="1+2"

To delete env variable set it to empty value::

  set VAR=

To load first line from a file into a variable::

  cmd> set /p VAR=<FILE

CMD tricks
==========
::

  $ set /p TOOLOUTPUT= < temp.txt

  $ for /f "tokens=*" %%i in ('%~dp0sometool.exe') do set TOOLOUTPUT=%%i

  $ for /f "tokens=1 delims=" %%s in (users.txt) do (echo %%S & command "%%S") >> outputfile.txt

Limits.
=======

Variable value and one line command string after expansion can not exceed 8191
characters for Windows XP and later and 2047 for Windows NT, Windows 2000.

http://support.microsoft.com/default.aspx?scid=kb;en-us;830473
  Command prompt (Cmd. exe) command-line string limitation.

How run cmd on 64-bit OS.
=========================

From 64-bit process::

  %windir%\System32\cmd.exe (for 64-bit)
  %windir%\SysWOW64\cmd.exe (for 32-bit)

From 32-bit process::

  %windir%\System32\cmd.exe (for 32-bit)
  %windir%\Sysnative\cmd.exe (for 64-bit)

http://msdn.microsoft.com/en-us/library/aa384187%28VS.85%29.aspx
  File System Redirector