Skip to content

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

Closed

Description

@vervaekejonathan

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

Activity

  1. crumblingstatue commented on Mar 28, 2022

    @crumblingstatue
    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.

  2. jfrimmel commented on Mar 28, 2022

    @jfrimmel
    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();
  3. wwylele commented on Mar 28, 2022

    @wwylele
    Contributor

    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.

  4. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    and removed
    C-bugCategory: This is a bug.
    on Sep 8, 2023
  5. Milo123459 commented on Sep 25, 2023

    @Milo123459
    Contributor

    so is this just a documentation change?

    @rustbot claim

  6. added a commit that references this issue on Oct 4, 2023
    d5bd019
  7. added a commit that references this issue on Oct 4, 2023
    1d85eb7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-processArea: `std::process` (note: `std::env` is A-env)C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions