Search code examples
windowsbatch-fileescapingcallpipe

Escape PIPE character in a function call windows Batch script


I am writing a function to execute shell commands and capture its output in a batch script.

:runShellCmd
   setlocal EnableDelayedExpansion
   SET lf=-
   FOR /F "delims=" %%i IN ('%~1') DO if "%out%" == "" (set out=%%i) else (set out=!out!%lf%%%i)
   echo "Cmd output: %out%"
   SET "funOut=%out%"
ENDLOCAL & IF "%~1" NEQ "" SET %~2=%out%
goto :EOF

I have been successful in passing simple commands and getting output. But for calls like

CALL :runShellCmd "echo Jatin Kumar | find /c /i "jatin"" it fails with error unexpected | character.

I know we need to escape | with ^ in for but if I try to pass ^| in the function argument string, it changes it to ^^| which again throws error.

Am I missing something?


Solution

  • This is an effect of the CALL command.

    The CALL command doubles all carets in one of the batch parser phases.
    Normally you wouldn't see this, as the carets will be used as an escape charater directly after the doubling.

    See this

    call echo ^^^^
    call call echo ^^^^
    call call call echo ^^^^
    
    call echo "^^^^"
    call call echo "^^^^"
    call call call echo "^^^^"
    

    Output

    ^^
    ^^
    ^^
    "^^^^^^^^"
    "^^^^^^^^^^^^^^^^"
    "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
    

    But how do you can escape your pipe character?
    You can't!

    But you can add a caret remover in your function.

    :runShellCmd
       setlocal EnableDelayedExpansion
       set "param=%~1"
       set param
       set "param=!param:^^=^!"
       for .... ('!param!')
    

    Or you could use an escaping trick when calling your function.

    set "caret=^"
    CALL :runShellCmd "echo Jatin Kumar %%caret%%| find /c /i "
    

    This works, as the %%caret%% will be expanded after the CALL caret doubling phase.