在Rust中执行 `zsh -c “echo -n $ZSH_CUSTOM”` 的方法

huangapple go评论100阅读模式
英文:

How to execute `zsh -c "echo -n $ZSH_CUSTOM"` in Rust

问题

I am trying to execute the following zsh command in Rust using Command:

$ zsh -c "echo -n $ZSH_CUSTOM"
/home/steve/.oh-my-zsh/custom%
use std::process::Command;

fn main() {
    let mut zsh = Command::new("zsh");
    zsh.args(["-c", "echo -n $ZSH_CUSTOM"]);
    let output = zsh.output().unwrap();

    println!("{:?}", output);
}

However, for the above Rust code, I am getting an unexpected result:

$ cargo r -q
Output { status: ExitStatus(unix_wait_status(0)), stdout: "", stderr: "" }

Anyway to make the Rust code have the same output with that zsh command? Thanks in advance!

BTW, my environment:

$ zsh --version
zsh 5.9 (x86_64-redhat-linux-gnu)

$ uname -a
Linux localhost 6.3.4-101.fc37.x86_64 #1 SMP PREEMPT_DYNAMIC Sat May 27 15:09:40 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

$ rustc --version
rustc 1.69.0 (84c898d65 2023-04-16)

If it matters.

英文:

I am trying to execute the following zsh command in Rust using
Command:

$ zsh -c "echo -n $ZSH_CUSTOM"
/home/steve/.oh-my-zsh/custom%
use std::process::Command;

fn main() {
    let mut zsh = Command::new("zsh");
    zsh.args(["-c", "echo -n $ZSH_CUSTOM"]);
    let output = zsh.output().unwrap();

    println!("{:?}", output);
}

However, for the above Rust code, I am getting an unexpected result:

$ cargo r -q
Output { status: ExitStatus(unix_wait_status(0)), stdout: "", stderr: "" }

Anyway to make the Rust code have the same output with that zsh command? Thanks in advance!

BTW, my environment:

$ zsh --version
zsh 5.9 (x86_64-redhat-linux-gnu)

$ uname -a
Linux localhost 6.3.4-101.fc37.x86_64 #1 SMP PREEMPT_DYNAMIC Sat May 27 15:09:40 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

$ rustc --version
rustc 1.69.0 (84c898d65 2023-04-16)

If it matters.

答案1

得分: 2

看起来 ZSH_CUSTOM 变量未被你的Shell导出。在运行程序之前,尝试执行 export ZSH_CUSTOM

问题在于当你在交互式Shell中运行 zsh -c "echo -n $ZSH_CUSTOM" 时,ZSH_CUSTOM 变量会被_交互式Shell_ 展开,然后它调用 zsh -c "echo -n /home/steve/.oh-my-zsh/custom"。当你从Rust中调用它(或者使用单引号调用 zsh -c 'echo -n $ZSH_CUSTOM' ),然后_被调用的_Shell 试图展开 ZSH_CUSTOM,但由于它未被导出,展开为空字符串。

英文:

Looks like the ZSH_CUSTOM variable is not exported by your shell. Try to do export ZSH_CUSTOM before running your program.

What happens is that when you run zsh -c "echo -n $ZSH_CUSTOM" in an interactive shell, the ZSH_CUSTOM variable is expanded by the interactive shell, which then calls zsh -c "echo -n /home/steve/.oh-my-zsh/custom". When you call it from Rust (or if you call zsh -c 'echo -n $ZSH_CUSTOM' with single quotes), then the called shell attempts to expand ZSH_CUSTOM, but since it's not exported the expansion is an empty string.

huangapple
  • 本文由 发表于 2023年6月1日 14:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定