Size: 1402
Comment:
|
Size: 659
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 68: | Line 42: |
This prints `%MyVar%`. The same behavior will be seen internally, as in conditional statement. |
Batch File Variables
Contents
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%'