3

I want to print variable in the console. But, I can watch %variable_name%...

This is an example code which can explain my situation.

Set a=%b% and i respect you, answer!
Set b=lolol i'm happy
Echo %a%

What I want::

lolol i'm happy and i respect you, answer!

Results::

%a% and i respect you, answer!

How can i solve this problem?? (I updated my question, sorry...)

JustTemp
  • 65
  • 1
  • 5
  • Not a batch expert by any means, but I'd suspect the variables get expanded when seen. I.e. as written `SET a=%b%` would have to appear after `b` has been set. If batch has an indirect reference, it'd very likely have a different syntax for that, but there I am out of my depth. Perhaps [this](https://stackoverflow.com/questions/9369874/windows-batch-programming-indirect-nested-variable-evaluation) helps? – Ondrej K. Sep 09 '18 at 16:07
  • 2
    Possible duplicate of [Windows batch programming: Indirect/nested variable evaluation](https://stackoverflow.com/questions/9369874/windows-batch-programming-indirect-nested-variable-evaluation) – Ondrej K. Sep 09 '18 at 16:13

1 Answers1

4

First, you need the percent signs in the content of the variable a. Double them to tell the parser that you want them literal instead of the (current) content of b.
Then you need another layer of expansion to evaluate %a% (variable) to %b% (literal with the percent signs) then to evaluate that %b% to the content of b:

Set a=%%b%% and i respect you, answer!
Set b=lolol i'm happy
call Echo %a%
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 2
    Note that on the command line, as opposed to a batch script, we can't escape percent by doubling it. The best we can do is to disrupt matching the name `b` by inserting a `^` escape character, e.g. `set a=%^b%`. This would match an environment variable named `^b`, but it's unlikely for this to be defined. – Eryk Sun Sep 09 '18 at 16:31
  • Thanks a lot!!! This solution can run in this situatuon? ::set a=%%b%% is true set b=lolol call echo %a% – JustTemp Sep 09 '18 at 16:37
  • @eryksun, There is even a better way which is guaranteed to work in command line. `set "b="`. So I guess using `%^b%` is not the best we can do ;) – sst Sep 10 '18 at 06:13
  • @sst, I don't follow you, `set "b="` clears the value of `b`. We're trying to set `a` to the literal value `%b%`. To do that we have to disrupt the first pass of the parser by having it look for a variable named `^b` instead, which we hope is not defined. Then in the second pass of the parser, the `^` escape character is removed since it's not in a quoted string, and we're left with the literal `%b%` to be assigned. – Eryk Sun Sep 10 '18 at 16:07
  • @eryksun, `to disrupt the first pass of the parser by having it look for a variable named ^b instead, which we hope is not defined` That is exactly the purpose of `set "b="`. we clear the value of `b` before using `set "a=%b%"`, so instead of betting on `^b` to be undefined, we can just make sure that `b` is undefined. Therefor there is no need to use `%^b%`, we just `%b%` with confidence. If you want to pass the command to subshell, just undefined `b` in the parent then pass `Set "a=%b%"` to subshell. (Just as an example) – sst Sep 10 '18 at 18:58
  • @sst, if `b` is already defined you'll have to copy it to a temp variable. This gets complicated if we're using this technique to escape many variable names in an expression. I still think it's better to use `^` since in practice I've never seen environment variable names use this character. Of course this only applies to interactive use on the command line. In a batch script we can use `%%` with confidence AFAIK. – Eryk Sun Sep 10 '18 at 21:17
  • 1
    @eryksun @sst, On the command line, I use `set "Perc=%" and then set" a=%Perc%b%Perc%"` – jeb Sep 11 '18 at 05:18
  • @jeb, you've beat me to the ground :) – sst Sep 11 '18 at 16:03
  • Thank you!! I've had this trouble for a week! – JustTemp Sep 13 '18 at 09:59