4

I am trying to call a batch file from another batch file and pass an argument containing a caret ^. Here's a minimal example:

@REM @echo off
call composer.bat require "psr/log:^3.0"

However, the call command is escaping the caret which is causing the command to fail:

In VersionParser.php line 526:
  Could not parse version constraint ^^3.0: Invalid version string "^^3.0" 

I tried removing the double quotes from psr/log:^3.0, but this is causing the caret to be removed entirely.

How do I prevent call from doubling the caret?

5
  • Python programming questions should be asked on Stack Overflow. Commented yesterday
  • 2
    how does a php script come into this picture? strangely the carat is coming through both batch files and getting to PHP, so the problem is probably that the second bat file needs to escape the character in a way that php understands (probably using backslash instead of ^) Commented yesterday
  • As a workaround, can you simply rename the command to not use a caret ? (no, I'm not a coder) Commented 14 hours ago
  • 1
    @Ramhound I never mentioned Python. Batch files are windows command-line scripts. Commented 6 hours ago
  • @CardinalSystem - Your question contains an error message from a Python script. Commented 1 hour ago

1 Answer 1

7

How do I prevent call from doubling the caret?

The doubling is due to a bug in Call - Windows CMD - SS64.com:

If the CALL command contains a caret character within a quoted string "test^ing" , the carets will be doubled.

See Phase 6) CALL processing/Caret doubling for all the gory details.


A messy solution

Use a variable to store the problem string (1).

first.cmd:

@echo off 
echo first.cmd
set var=psr/log:^^test3.0
echo parameter to pass is "%var%"
echo calling second.cmd
call second.cmd "%%var%%"

second.cmd:

@echo off
echo second.cmd
echo parameter 1 is %1

output:

G:\test>first
first.cmd
parameter to pass is "psr/log:^test3.0"
calling second.cmd
second.cmd
parameter 1 is "psr/log:^test3.0"

G:\test>

(1) https://stackoverflow.com/a/41770230

(2) https://stackoverflow.com/a/41770988 has a few other workarounds

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.