@Echo off is not working when Piping batch code to cmd

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 81
Joined: 30 Jun 2014 04:10

@Echo off is not working when Piping batch code to cmd

#1 Post by BoQsc » 15 Mar 2020 03:58

I would like to know if there is any known way to turn off Echo when piping batch code to cmd.

Only by adding /Q option to cmd.exe, the output is actually with echo off.

I need the script to be the trigger of echo off, and not the /Q option of cmd.exe

If not, Are there any alternative ways to execute code from standard output by piping except for cmd?


Working example:

Code: Select all

curl vaido.world | cmd
Output:

Code: Select all

title New
echo "yessf"
"yessf"
cmd with /Q option

Code: Select all

curl vaido.world | cmd /Q
Output:

Code: Select all

"yessf"

ShadowThief
Expert
Posts: 963
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: @Echo off is not working when Piping batch code to cmd

#2 Post by ShadowThief » 15 Mar 2020 04:18

Why don't you want to use cmd /Q? That's how you do it.

BoQsc
Posts: 81
Joined: 30 Jun 2014 04:10

Re: @Echo off is not working when Piping batch code to cmd

#3 Post by BoQsc » 15 Mar 2020 04:28

ShadowThief wrote:
15 Mar 2020 04:18
Why don't you want to use cmd /Q? That's how you do it.
You see typing additional options are hard.
They are hard to remember,
takes aditional time for beginners that are only starting to use command line prompt, and they will use this command only few times in years.

I'm starting to think that I should just simply use @ at the beginning of every command.
But that will add more complexity and readability problems.

ShadowThief
Expert
Posts: 963
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: @Echo off is not working when Piping batch code to cmd

#4 Post by ShadowThief » 15 Mar 2020 04:31

Then I suggest you write a wrapper script that takes the URL you're downloading as %1 and pipes that to cmd /q

BoQsc
Posts: 81
Joined: 30 Jun 2014 04:10

Re: @Echo off is not working when Piping batch code to cmd

#5 Post by BoQsc » 15 Mar 2020 04:50

ShadowThief wrote:
15 Mar 2020 04:31
Then I suggest you write a wrapper script that takes the URL you're downloading as %1 and pipes that to cmd /q
Wouldn't it be possible without wrapper script?
'
There are no ways to enclose code and pipe it into cmd /q like in this pseudo code?

Non-working pseudo-code

Code: Select all

(
echo Hello this is going to be piped to cmd /q
echo everything will work correctly
echo this is only pseudo-code

) | cmd.exe /Q

Uh I quickly came up with poorly looking and almost unreadable but seemingly working example that has no advantage over @ prefixing way.

Code: Select all

@echo off
(
echo @echo off
echo cls
echo echo Hello this is going to be piped to cmd /q
echo echo everything will work correctly
echo echo this is only pseudo-code

) | cmd.exe /q

pause


If only there were no requirement to prefix every command with echo this would be perfect.

And it actually works, however, requires tiring mundane prefixation of echo. Which is sad.

Code: Select all

curl vaido.world/example/ | cmd
Last edited by BoQsc on 15 Mar 2020 06:17, edited 1 time in total.

ShadowThief
Expert
Posts: 963
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: @Echo off is not working when Piping batch code to cmd

#6 Post by ShadowThief » 15 Mar 2020 06:06

That's fair, if you could put the script on the machine that the code needed to run on, you wouldn't need to download it with curl and pipe it to cmd...

In that case, I can recommend a two-part solution:

Code: Select all

curl vaido.world -o runme.bat && call runme.bat
Or a three-part solution if you want to clean up after yourself:

Code: Select all

curl vaido.world -o runme.bat && call runme.bat && del runme.bat
That's about as close as you're going to get without needing to remember that cmd has a /Q flag.

jeb
Expert
Posts: 964
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: @Echo off is not working when Piping batch code to cmd

#7 Post by jeb » 16 Mar 2020 01:16

Hi BoQs,
BoQsc wrote:
15 Mar 2020 04:50
If only there were no requirement to prefix every command with echo this would be perfect.
There is no need for the ECHO prefix of each line, it's only necessary in your example.

You can download and start a simple script with a pipe.

Test.bat

Code: Select all

@echo off
(
cls

echo Hello
echo Time: !time!
exit

)
And execute it with

Code: Select all

curl -s https://example.com/test.bat | cmd /v:on /k
The code has to follow more or less the rules for batch macros.


A echo off or @ doesn't work, the commands are still displayed, because it's like typing them into cmd.
The only way to hide them is to use /Q or to clear the output with CLS

jeb
Expert
Posts: 964
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: @Echo off is not working when Piping batch code to cmd

#8 Post by jeb » 16 Mar 2020 04:22

If you are on windows10 with ansi escape sequences enabled, you can use some magic to hide the commands.

Code: Select all

cmd /Q /v:on /k
for /F "delims=#" %a in ('"prompt #$E# & for %a in (1) do rem"') do set "ESC=%a" & echo off
<nul set /p ".=%ESC%[G%ESC%[2K%ESC%%ESC%[A%ESC%[2K"
echo #1
echo #2
echo #3

echo !time!
echo ----1
echo ----2
echo ----3
exit

Even without escape sequences, only the first two lines are visible

Aacini
Expert
Posts: 1645
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: @Echo off is not working when Piping batch code to cmd

#9 Post by Aacini » 17 Mar 2020 15:40

The simplest way is via the cmd /Q trick, but this don't omits the initial text. You may review other differences in the code executed via a .BAT batch file vs. via text redirected into cmd.exe at cmd.exe < NotBatch.txt file topic. For example:

- @ECHO OFF command just supresses the echoing of the prompt and FOR iterations. To supress the echoing of all commands, the NotBatch.txt file must be executed this way: "cmd.exe < NotBatch.txt > NUL" and then use "echo Text > CON" in order to display text in the screen.

Antonio

Post Reply