Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatted String #10323

Closed
xmha97 opened this issue Jan 20, 2023 · 1 comment
Closed

Formatted String #10323

xmha97 opened this issue Jan 20, 2023 · 1 comment

Comments

@xmha97
Copy link

xmha97 commented Jan 20, 2023

Description

In the .cmd/.bat script files on Windows, the --write-out option does not work and we have to use two percent signs (%%) instead of one percent signs (%) in the formatted string.

For example, the following command does not work:

curl --url "http://cachefly.cachefly.net/1mb.test" -qfsS --write-out "%{speed_download}\n" -o NUL

But this command works:

curl --url "http://cachefly.cachefly.net/1mb.test" -qfsS --write-out "%%{speed_download}\n" -o NUL

Please mention this point in this article because my friend thought that the Windows build of cURL has a problem:
https://everything.curl.dev/usingcurl/verbose/writeout
Thanks.

What is the cause of this problem?

Actually, there is no problem. Command Prompt variables comprise a % character followed by a name, for example:

FOR /f %f in ('dir /b .') DO ECHO %f
jay added a commit to jay/curl that referenced this issue Jan 23, 2023
- Clarify that in Windows batch files the % must be escaped as %%, and
  at the command prompt it cannot be escaped which could lead to
  incorrect expansion.

Prior to this change the doc implied % must be escaped as %% in win32
always.

---

Examples:

If curl --write-out "%{http_code}" is executed in a batch file:
{http_code}

If curl --write-out "%%{http_code}" is executed in a batch file:
%{http_code}

If curl --write-out "%{http_code}" is executed from the command prompt:
%{http_code}

If curl --write-out "%%{http_code}" is executed from the command prompt:
%%{http_code}

At the command prompt something like "%{speed_download}%{http_code}"
would first be parsed by the command interpreter as %{speed_download}%
and would be expanded as environment variable {speed_download} if it
existed, though that's highly unlikely since Windows environment names
don't use braces.

---

Reported-by: Muhammad Hussein Ammari

Fixes curl#10323
Closes #xxxx
jay added a commit to jay/everything-curl that referenced this issue Jan 23, 2023
- Clarify that in Windows batch files the % must be escaped as %%, and
  at the command prompt it cannot be escaped which could lead to
  incorrect expansion.

Prior to this change the doc implied % must be escaped as %% in win32
always.

---

Examples:

If curl --write-out "%{http_code}" is executed in a batch file:
{http_code}

If curl --write-out "%%{http_code}" is executed in a batch file:
%{http_code}

If curl --write-out "%{http_code}" is executed from the command prompt:
%{http_code}

If curl --write-out "%%{http_code}" is executed from the command prompt:
%%{http_code}

At the command prompt something like "%{speed_download}%{http_code}"
would first be parsed by the command interpreter as %{speed_download}%
and would be expanded as environment variable {speed_download} if it
existed, though that's highly unlikely since Windows environment names
don't use braces.

---

Reported-by: Muhammad Hussein Ammari

Ref: curl/curl#10337

Fixes curl/curl#10323
Closes #xxxx
@jay
Copy link
Member

jay commented Jan 23, 2023

In the .cmd/.bat script files on Windows, the --write-out option does not work and we have to use two percent signs (%%) instead of one percent signs (%) in the formatted string.

The manual says:

NOTE: The %-symbol is a special symbol in the win32-environment, where all occurrences of % must be doubled when using this option.

The book says:

(The %-symbol is special on the Windows command line, where all occurrences of % must be doubled when using this option.)

I think we could make it clearer though. The escaping is needed for batch files but cannot be done if the command is typed at the prompt. I've submitted two PRs, one for the book and one for manpage.

#10337
bagder/everything-curl#279

jay added a commit that referenced this issue Jan 26, 2023
- Clarify that in Windows batch files the % must be escaped as %%, and
  at the command prompt it cannot be escaped which could lead to
  incorrect expansion.

Prior to this change the doc implied % must be escaped as %% in win32
always.

---

Examples showing how a write-out argument is received by curl:

If curl --write-out "%{http_code}" is executed in a batch file:
{http_code}

If curl --write-out "%%{http_code}" is executed in a batch file:
%{http_code}

If curl --write-out "%{http_code}" is executed from the command prompt:
%{http_code}

If curl --write-out "%%{http_code}" is executed from the command prompt:
%%{http_code}

At the command prompt something like "%{speed_download}%{http_code}"
would first be parsed by the command interpreter as %{speed_download}%
and would be expanded as environment variable {speed_download} if it
existed, though that's highly unlikely since Windows environment names
don't use braces.

---

Reported-by: Muhammad Hussein Ammari

Ref: bagder/everything-curl#279

Fixes #10323
Closes #10337
bch pushed a commit to bch/curl that referenced this issue Jul 19, 2023
- Clarify that in Windows batch files the % must be escaped as %%, and
  at the command prompt it cannot be escaped which could lead to
  incorrect expansion.

Prior to this change the doc implied % must be escaped as %% in win32
always.

---

Examples showing how a write-out argument is received by curl:

If curl --write-out "%{http_code}" is executed in a batch file:
{http_code}

If curl --write-out "%%{http_code}" is executed in a batch file:
%{http_code}

If curl --write-out "%{http_code}" is executed from the command prompt:
%{http_code}

If curl --write-out "%%{http_code}" is executed from the command prompt:
%%{http_code}

At the command prompt something like "%{speed_download}%{http_code}"
would first be parsed by the command interpreter as %{speed_download}%
and would be expanded as environment variable {speed_download} if it
existed, though that's highly unlikely since Windows environment names
don't use braces.

---

Reported-by: Muhammad Hussein Ammari

Ref: bagder/everything-curl#279

Fixes curl#10323
Closes curl#10337
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants