What are% 1, \ 1, and $ 1?(What is, Concept and Definition)

When used in a command line, script, or batch file, % 1 is used to represent a variable or matching string.For example, in a Microsoft batch file,% 1 can be used to print what is entered after the batch file name.


In the example below, using% 1, the batch file will print "Hello xxxx, nice to meet you", where xxxx is what you enter after the batch file name.So if this batch file is named it example.bat and you typed example Nathan , the batch file would return "Hi Nathan, nice to meet you".

@echo off if % 1 ==goto error echo Hello% 1 it is a pleasure to meet you goto end: error echo write your name after the batch file: end

In other programming languages ​​and scripting languages,% 1 can be replaced by \ 1 or $ 1 .For example, in Perl, these could be used in a regular expression to print the matching text, or used as a new variable.In the example below, if the variable $ text contains some text, it will print "Hello xxxx" where xxxx is what matches.So if $ text= Joe Smith , the script would return "Hello Joe".

if ($ text=~ s/^ ([az] +)/i ) {print Hello $ 1 \ n ;
}

Each of these matching strings or variables can also be extended by increasing the value.For example, the following matching string or matching variable could be entered as% 2, \ 2, or $ 2.In the above batch file example , you can add a% 2 to also print the last name as shown in the example below.If the last name was not entered,% 2 will not print anything.

echo Hello% 1% 2 it is a pleasure to meet you

In the case of the Perl example above, adding $ 2 would print the second matching string in parentheses, as shown below.

if ($ text=~ s/^ ([az] +) ([az] + )/i) {print Hello $ 1 $ 2 \ n ;
}

You can also add additional matching strings or variables using 3, 4, 5, etc.(for example,% 3,% 4, or $ 3, $ 4).

Comments