英文:
Run shell command with "redirecting of an output from a subshell" statement within go
问题
根据在新的shell中运行bash命令并在该命令执行后保留在新的shell中,我该如何在Golang中运行以下命令:
bash --rcfile <(echo "export PS1='> ' && ls")
我尝试了许多exec.Command()
的组合,但它们都不起作用。例如:
exec.Command("bash", "--rcfile", `<("echo 'ls'")`)
我还阅读了这个os,os/exec:使用重定向符号'<' '>'失败,但我认为我的情况可能有点更复杂。
英文:
According to run bash command in new shell and stay in new shell after this command executes, how can I run command:
bash --rcfile <(echo "export PS1='> ' && ls")
within golang? I've tried many combinations of exec.Command()
but they did't work. For example:
exec.Command("bash", "--rcfile", `<("echo 'ls'")`)
I've also read this os, os/exec: using redirection symbol '<' '>' failed, but I think maybe my case is a bit more complicated.
答案1
得分: 3
你离成功很近了-我认为你的困惑在于你正在使用管道来调用bash,这意味着你实际上需要使用bash来调用bash:
exec.Command("bash", "-c", `bash --rcfile <(echo "export PS1='> ' && ls")`)
英文:
You're almost there - I think the confusion is you're using piping to call bash, which means you actually need to call bash using bash:
exec.Command("bash", "-c", `bash --rcfile <(echo "export PS1='> ' && ls")`)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论