Size: 1402
Comment:
|
Size: 1484
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
## page was renamed from BatchFile/Variables | |
Line 9: | Line 10: |
== Variables == | == Declaration == |
Line 11: | Line 12: |
Variables are defined with `set` and accessed by surrounding a variable's name with percent signs (`%`). Best practice is to quote variable substitutions. |
Variables are declared with the `set` [[Batch/BuiltinCommands#Set|builtin. |
Line 17: | Line 16: |
IF "%foo%"=="bar" ECHO Equal }}} === Escaping Special Character === Special characters are escaped using the4 caret (`^`). {{{ set foo=bar ^| baz }}} As with Unix scripting, when commands spawn subshells that re-interpret tokens, it becomes necessary to add an escaped escape character for each layer. {{{ echo foo ^^^& bar | subroutine }}} Double-quoted strings are also interpreted literally. === Escaping Double Quotes === ''Because'' double-quoted strings are interpreted literally, there is further complication for double-quoted strings containing double quotes. There is no universal solution as `cmd.exe` defers interpretation. Consult the specific commands to see if double quotes should be duplicated... {{{ set foo="foo ""bar"" baz" }}} ...or should be escaped in the Unix style... {{{ set foo="foo \"bar\" baz" |
|
Line 60: | Line 22: |
== Unset Variables == | == Usage == Variables are accessed by their name. A percent sign (`%`) must surround the name. {{{ set bar=%foo% }}} If a variable's value includes a character that the shell will interpret specially, [[Batch/Quoting|quote]] the variable. {{{ set bar="%foo%" }}} |
Line 66: | Line 40: |
echo %MyVar% | echo %MyVar% # '%MyVar%' |
Line 69: | Line 43: |
This prints `%MyVar%`. | ---- |
Line 71: | Line 45: |
The same behavior will be seen internally, as in conditional statement. | == Special Variables == ||'''Variable''' ||'''Value''' || ||`%CD%` ||current working directory, ending in a slash (`/`) unless in a drive root|| ||`%TIME%` ||system time in a localized format || ||`%DATE%` ||system date in a localized format || ||`%RANDOM%` ||a generated pseudorandom number between 0 and 32767 || ||`%ERRORLEVEL%` ||error level returned by the most recent command or script || ||`%CMDEXTVERSION%`||version of Command Processor Extensions used by `cmd.exe` || ||`%CMDCMDLINE%` ||command used to start `cmd.exe` || |
Batch File Variables
Declaration
Variables are declared with the set [[Batch/BuiltinCommands#Set|builtin.
set foo=bar
Usage
Variables are accessed by their name. A percent sign (%) must surround the name.
set bar=%foo%
If a variable's value includes a character that the shell will interpret specially, quote the variable.
set bar="%foo%"
If a variable is not set, no substitution occurs.
set MyVar= echo %MyVar% # '%MyVar%'
Special Variables
Variable |
Value |
%CD% |
current working directory, ending in a slash (/) unless in a drive root |
%TIME% |
system time in a localized format |
%DATE% |
system date in a localized format |
%RANDOM% |
a generated pseudorandom number between 0 and 32767 |
%ERRORLEVEL% |
error level returned by the most recent command or script |
%CMDEXTVERSION% |
version of Command Processor Extensions used by cmd.exe |
%CMDCMDLINE% |
command used to start cmd.exe |