2

I want to make something like this

set ac=&Hello

But it will say 'Hello' is not a command. I tried putting double quote like this,

set ac="&Hello"

But it will show the double quotes. Does anyone have an idea?

RajaEzzy
  • 25
  • 6
  • How about `set "ac=&Hello"`, and then output with delayed expansion? –  Aug 31 '17 at 05:35
  • @RajaEzzy Read the answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) which explains in detail how to assign a value to an environment variable right and why the syntax `set "variable=value"` should be used nearly always. – Mofi Aug 31 '17 at 05:41

2 Answers2

3

Using delayedExpansion, one can echo out any content, even special characters without escaping. Take a look at the follow example:

Longer but Clearer Solution

@echo off
setlocal enableDelayedExpansion
set "ac=&Hello"
echo !ac!

This will output &Hello as you expected. There are still some other methods, but this one is one of the simplest one.


Shorter but Messier Solution

@echo off
setlocal enableDelayedExpansion
set "ac=&Hello"&&echo !ac!

Simillar to the above, this snippet only moves the echo to the same line as the set statement. This will be easier to see when echoing a large content of text.


2

You can try this :

@echo off
set ab=^^^&hello
echo %ab% & pause
Hackoo
  • 18,337
  • 3
  • 40
  • 70