Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch echo pipe symbol causing unexpected behaviour

Tags:

batch-file

I have a variable in my batch file and it contains the pipe symbol (this one: |) so when I echo the variable I get an error about a unrecognized internal/external command.

I need a way to either get it to echo it correctly or better yet remove everything after and including the | symbol as well as any extra spaces before it.

like image 397
Hintswen Avatar asked Dec 06 '09 10:12

Hintswen


People also ask

What does echo mean in a batch file?

In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.

How do I turn off echo batch?

To prevent echoing a particular command in a batch file, insert an @ sign in front of the command. To prevent echoing all commands in a batch file, include the echo off command at the beginning of the file.

What is echo dot in batch script?

When writing Batch files ( . bat ), ECHO. is used to print a blank line to the screen.

How do I echo a newline batch?

To create a blank line in a batch file, add an open bracket or period immediately after the echo command with no space, as shown below. Adding @echo off at the beginning of the batch file turns off the echo and does not show each of the commands. @echo off echo There will be a blank line below. echo.


1 Answers

There are several special characters that generally must be escaped when used in Windows batch files. Here is a partial list: < > & | ^ %

The escape character is ^. So to get a literal |, you should do this:

echo ^| 

When the special character is in a variable, it becomes a bit harder. But if you use special syntax, you can replace characters in a variable like this:

set X=A^|B  REM replace pipe character with underscore set Y=%X:|=_%  echo %Y% REM prints "A_B" 
like image 171
bobbymcr Avatar answered Oct 16 '22 08:10

bobbymcr