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

std::process::Command can't echo environment variables #95400

Closed
vervaekejonathan opened this issue Mar 28, 2022 · 4 comments · Fixed by #116146
Closed

std::process::Command can't echo environment variables #95400

vervaekejonathan opened this issue Mar 28, 2022 · 4 comments · Fixed by #116146
Assignees
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-process Area: `std::process` and `std::env` C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@vervaekejonathan
Copy link

vervaekejonathan commented Mar 28, 2022

I tried this code:

use std::process::Command;
...
    let s = Command::new("echo")
        .arg("$123")
        .output()
        .expect("command failed");
    println!("{}", String::from_utf8_lossy(&s.stdout));

I expected to see this happen:
Print the content of the $123 environment variable

Instead, this happened:
It literally prints $123

Meta

rustc --version --verbose:

rustc --version --verbose
rustc 1.61.0-nightly (63b8f01bb 2022-03-24)
binary: rustc
commit-hash: 63b8f01bb5ca277e7df8d7efe094ed4244c1790c
commit-date: 2022-03-24
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0
@vervaekejonathan vervaekejonathan added the C-bug Category: This is a bug. label Mar 28, 2022
@crumblingstatue
Copy link
Contributor

It is the shell that resolves environment variables. You're not using a shell here, just directly executing a command. This is behaving as expected.

@jfrimmel
Copy link
Contributor

Indeed, this is the expected behavior. This can be circumvented by actually using a shell, e.g. on POSIX:

Command::new("sh").arg("-c").arg("echo $123").output().unwrap();

@wwylele
Copy link
Contributor

wwylele commented Mar 28, 2022

I did a quick check on the doc for arg

Note that the argument is not passed through a shell, but given literally to the program. This means that shell syntax like quotes, escaped characters, word splitting, glob patterns, substitution, etc. have no effect.

I guess we can expand "substitution" into "variable substitution" to make it more clear.

@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@workingjubilee workingjubilee added the A-process Area: `std::process` and `std::env` label Jul 22, 2023
@fmease fmease added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. and removed C-bug Category: This is a bug. needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. labels Sep 8, 2023
@Milo123459
Copy link
Contributor

so is this just a documentation change?

@rustbot claim

@bors bors closed this as completed in d5bd019 Oct 4, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 4, 2023
Rollup merge of rust-lang#116146 - Milo123459:milo/clarify-arg-documentation, r=thomcc

Clarify `arg` and `args` documentation

Fixes rust-lang#95400
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-process Area: `std::process` and `std::env` C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants