1

I have written a Windows batch script to create an array and tried to print every element of the array.

However, the output is ECHO is off. instead of the actual value such as "Apple".

When I changed ECHO is off. to @echo on, the result has just changed to ECHO is on.

@echo off
    
Rem Create variable
set fruit[0]="apple"
set fruit[1]="orange"
set fruit[2]="pear"
set fruit[3]="lemon"
    
REM x is the last index of the array
set x=3
    
REM for loop [with 0 for start, 1 for step, x for end]
FOR /L %%a in (0,1,%x%) DO (
   call set var=%%fruit[%%a]%%
   set var
   echo %var%
   call echo "Text2" 
  )
 
pause

The command call set var=%%fruit[%%a]%% is correct because when the command set var is run, the output can successfully show something like var="lemon", which means the %var% has successfully stored the value of "lemon".
However, the following command echo %var% output is ECHO is off..

How does it happen?

The following is the batch script result capture from cmd. (@echo off)

var="apple"
ECHO is off.
"Text2"
var="orange"
ECHO is off.
"Text2"
var="pear"
ECHO is off.
"Text2"
var="lemon"
ECHO is off.
"Text2"
Press any key to continue . . .
2
  • 1
    How does it happen? Read about setlocal enabledelayedexpansion. – Akina Apr 9 at 9:22
  • As Akina points out, because delayedexpansion is not on, var is empty, so the command executed is echo which just prints what the status of echo is. – LPChip Apr 9 at 9:28
1
@echo off

setlocal enabledelayedexpansion

Rem Create variable
set fruit[0]="apple"
set fruit[1]="orange"
set fruit[2]="pear"
set fruit[3]="lemon"


REM x is the last index of the array
set x=3

REM for loop (with 0 for start, 1 for step, x for end)
FOR /L %%a in (0,1,%x%) DO (
   call set var=%%fruit[%%a]%%
   set var
REM   echo %var%
   echo !var!
   call echo "Text2" 
   )
)

endlocal

pause

Without enabledelayedexpansion variable value is always equal to one at the beginning of cycle. var is empty before so echo %var% is expanded to echo which prints current ECHO status.

With enabledelayedexpansion you can use not only %variable% but !variable! additionally. First one again provides the value at the start of a cycle whereas secopnd one provides actual value.

For to see this everything: add SET var=zero line immediately after @echo off command and execute initial batch file and my code (removing REM before echo %var%)

1
  • +1 although it would definitely be helpful to explain why. :) – LPChip Apr 9 at 9:33
0

> echo /?
Displays messages, or turns command-echoing on or off. 
 
ECHO [ON | OFF] 
ECHO [message] 
 
Type ECHO without parameters to display the current echo setting.  


The echo command can display messages, variables and/or messages and variables on the screen, but it can also return the current status to the echo command, since this is a definition for the behavior of to send (or echo) action.

To one or more commands in your script/prompt, which will not occur when set to omit them (echo off).

Since your variable does not have a value (at run time), your %variable% == "" (nothing, no strings/characters), so, the cmd.exe (command line interpreter) will execute the command echo %var% as echo (only).

The echo off however assigned to the entire script, made at the beginning of it, as soon as the command echo noting |or| echo is used, (as it happens in its variable with no assigned value/var to display), it will display the current status for the echo command, as there are no strings to display on the screen.

  • For testing/debugging purposes, you can avoid these messages using:
echo. %var%
echo; %var%
echo/ %var%
echo\ %var%
echo= %var%
echo+ %var%
echo( %var%
echo[ %var%
echo: %var%

Since there is no value in the runtime variable, any of the above echo * commands returns a blank line:


your code... 
echo= %var%
...
  • Returns:
var="apple"

"Text2"
var="orange"

"Text2"
var="pear"

"Text2"
var="lemon"

"Text2"
Press any key to continue . . .

  • To set and use/access your array index items, you can use:

Split String Into Substrings Based on Delimiter / dostips.com

@echo off

setlocal enabledelayedexpansion
set "_array=apple,orange,pear,lemon"

set "_array_0=%_array:,=" & set /a _i+=1 & set "_array_!_i!=%"
for /l %%i in (0 1 !_i!)do echo:_array_%%~i == "!_array_%%~i!"

%__AppDir__%TimeOut.exe /t 000000010 /nobreak | <con: endlocal
  • Results:
_array_0 == "apple"
_array_1 == "orange"
_array_2 == "pear"
_array_3 == "lemon"

Obs.: It is good practice to use the endlocal when it is no longer needed.


  • Some alternative using loops to set array/index value:
@echo off && setlocal enabledelayedexpansion

set "_var=apple,orange,pear,lemon" && set/a "_i=100000-100001"
for %%i in (!_var!)do set/a "_i+=1"&&>con set "_var_!_i!=%%~i"
    
for /L %%L in (00,01,0!_i!)do echo\ _var_%%~L == "!_var_%%~L!"
%__AppDir__%TimeOut.exe /t 000000010 /nobreak | <con: endlocal
  • Results:
 _var_0 == "apple"
 _var_1 == "orange"
 _var_2 == "pear"
 _var_3 == "lemon"

Additional resources:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.