Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Was ECHO ON or OFF when my Windows .bat was CALLed?

When I CALL one Windows .bat file from another, can the called file tell whether ECHO was ON or OFF in the caller?

When a .bat file is entered, ECHO is set to ON (even if it was OFF in the CALLER).

I want to preserve the caller's ECHO status in the called .bat so I can control both (possibly a chain of more than two nested calls) from a single place.

I could pass a parameter or set my own environment variable but is there a better way?

EDIT Just to clarify what's needed: callee.cmd needs to do things with echo off only for the duration of its run. Upon returning to its caller, it must restore the echo state the caller had.

Please fill in the ?????s in the script below:

callee.cmd:

@rem save the echo state
@ ???? HOW???? ????
@ set "savedEchoState=??????"

@rem the following commands must not be echoed
@echo off
command1
command2
command3

@rem restore the previous echo state
echo %savedEchoState%

caller1.cmd:

@echo off
call callee.cmd
echo

caller2.cmd:

@echo on
call callee.cmd
echo

Required output:

caller1.cmd must print echo off and caller2.cmd must print echo on.

Can it be done without creating any files on a disk?

like image 686
Denis Howe Avatar asked Oct 17 '25 14:10

Denis Howe


1 Answers

This is a tough one. Everything involving a pipe or a for doesn't work, as new processes get generated, which invalidates the echo status. It is possible involving a temp file though:

A.BAT:

echo on
echo
call b.bat
echo off
echo
call b.bat
echo

B.BAT:

@(>tmp echo)
@type tmp|find "(ON)" >nul && (@set "oldEcho=ON") || (@set "OldEcho=OFF")
@echo off
echo B:echo was previously %OldEcho%
REM insert your payload here
REM restore previous Echo Status:
echo %OldEcho%

Result:

C:\Folder>a

C:\Folder>echo on

C:\Folder>echo
ECHO ist eingeschaltet (ON).

C:\Folder>call b.bat
B:echo was previously ON

C:\Folder>echo off
ECHO ist ausgeschaltet (OFF).
B:echo was previously OFF
ECHO ist ausgeschaltet (OFF).

This makes use of the (unintuitive) fact that the on/off message is language dependent (German in my example above), but ends with an English suffix (ON) or (OFF) (which should be consistent in every language; if you know a language that does not do this, please let us know)

Edit (Nov-2023): I just realized (now having access to an English Windows), the output of echo in English is ECHO is on./ECHO is off., invalidating the above solution (find "(ON)".

I guess replacing find with findstr /i "\<ON\>" (searching including word boundaries) should solve that, and also avoid false positives (now because /i) with things like ECHO eston deactivon (OFF) (as in @jeb's comment).

Still not sure, if it's bulletproof (working for every language). If you stumble over a language that makes it fail, please @notify me.

like image 147
Stephan Avatar answered Oct 20 '25 05:10

Stephan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!