ChatGPT解决这个技术问题 Extra ChatGPT

Defining and using a variable in batch file

I'm trying to define and use a variable in a batch file. It looks like it should be simple:

@echo off

set location = "bob"
echo We're working with "%location%"

The output I get is the following:

We're working with ""

What's going on here? Why is my variable not being echo'd?


B
Brian Nixon

The space before the = is interpreted as part of the name, and the space after it (as well as the quotation marks) are interpreted as part of the value. So the variable you’ve created can be referenced with %location %. If that’s not what you want, remove the extra space(s) in the definition.


If the space before = is interpreted as part of the name then the variable cannot be referenced as %location%, it has to be referenced as %location %. Is that a typo?
@Miserable Variable: You’re right; it seems the original answer fell victim to an incorrect edit.
So this is what MS came with
@Falaque As opposed to alias x = y in *nix land?
@Falaque so what do you think bash will do when running x = y?
d
dbenham

The spaces are significant. You created a variable named 'location ' with a value of
' "bob"'. Note - enclosing single quotes were added to show location of space.

If you want quotes in your value, then your code should look like

set location="bob"

If you don't want quotes, then your code should look like

set location=bob

Or better yet

set "location=bob"

The last syntax prevents inadvertent trailing spaces from getting in the value, and also protects against special characters like & | etc.


Are there any downsides for the last syntax?
@Wolf - No, there is no downside. However, there is a limit to when it is most effective. As long as the value does not contain quotes, then almost any characters can appear in the value without worrying about escaping. The exceptions are %, and possibly ! if delayed expansion is enabled. But if the value does contain quotes, then you must worry about when to escape poison characters like |, &, etc.
this seems to be same as setting the environment-variable in windows. Is this the only way of using local-variables in batch scripts?
@Wolf, you only have to ensure that the command extensions are enabled, but this is the default configuration anyway...
P
Philip

input location.bat

@echo off
cls

set /p "location"="bob"
echo We're working with %location%
pause

output

We're working with bob

(mistakes u done : space and " ")


set /p prompts the user and stores the answer in the specified variable. e.g., set /p name="What is your name? " & echo Hello, %name%.
-1, The syntax set /p "varname"="text" is wrong, it creates a variable named location" (with a trailing quote!). It should be set /p location="bob". But even then, it doesn't match the question anymore
s
surfmuggle

Consider also using SETX - it will set variable on user or machine (available for all users) level though the variable will be usable with the next opening of the cmd.exe ,so often it can be used together with SET :

::setting variable for the current user
if not defined My_Var (
  set "My_Var=My_Value"
  setx My_Var My_Value
)

::setting machine defined variable
if not defined Global_Var (
  set "Global_Var=Global_Value"
  SetX Global_Var Global_Value /m
)

You can also edit directly the registry values:

User Variables: HKEY_CURRENT_USER\Environment

System Variables: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Which will allow to avoid some restrictions of SET and SETX like the variables containing = in their names.

3rd party edit

SETX.exe Set environment variables permanently, SETX can be used to set Environment Variables for the machine (HKLM) or currently logged on user (HKCU):

Option /m

 /m
       Set the variable in the system environment HKLM.
       (The default is the local environment HKCU)

Another example

::setting variable for the current user
if not defined JAVAJDK (
  set "JAVAJDK=C:\Program Files\Java\jdk-13\bin"
  setx JAVAJDK "C:\Program Files\Java\jdk-13\bin"
)

In a command.exe you can use the variable like this cd %JAVAJDK%.