Differences between revisions 1 and 6 (spanning 5 versions)
Revision 1 as of 2022-05-11 17:30:17
Size: 1402
Comment:
Revision 6 as of 2023-01-30 18:30:25
Size: 1732
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''' ||
||`%#` ||number of arguments ||
||`%$` ||all arguments as a string ||
||`%*` ||all arguments as a string, unmodified by the `shift` [[Batch/BuiltinCommands#Shift|builtin]]||
||`%@` ||all arguments as a string, but each token is [[Batch/Quoting|quoted]] ||



=== Positional Variables ===

The name of a command is stored in `%0`.

The first argument to the command is stored in `%1`. And so on until the 9th argument, `%9`.

For additional arguments, use the `shift` [[Batch/BuiltinCommands#Shift|builtin]] to move them into the 9 accessible variables.

To get all arguments as a string (like `%$`) starting from index ''n'', try `%n$`. To get all arguments until index ''n'' (non-inclusive), try `%-n$`.

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

%#

number of arguments

%$

all arguments as a string

%*

all arguments as a string, unmodified by the shift builtin

%@

all arguments as a string, but each token is quoted

Positional Variables

The name of a command is stored in %0.

The first argument to the command is stored in %1. And so on until the 9th argument, %9.

For additional arguments, use the shift builtin to move them into the 9 accessible variables.

To get all arguments as a string (like %$) starting from index n, try %n$. To get all arguments until index n (non-inclusive), try %-n$.


CategoryRicottone

Batch/Variables (last edited 2023-01-30 18:52:07 by DominicRicottone)