我已经编写了名为 MyShell 的 cpp 应用程序,它将一些真实的 shell 名称(通常是 bash)及其参数作为参数。MyShell 用作它的包装器。
我需要更改内壳的命令提示,特别是 PS1 环境变量。
我知道如何使用 PS1 env var 以命令行方式进行操作:
$ PS1="[myshell]"$PS1
[myshell]$
但是从 cpp 应用程序中做到这一点并不容易:
string newPS1 = "[myshell]" + string(getenv("PS1"));
setenv("PS1", newPS1.c_str());
if (execvp(shell, argv) < 0) {
cerr << "can not exec " << shell << ": " << strerror(errno) << endl;
exit(1);
}
afaik,当 bash 被调用时,它从 /etc/.bashrc 或 /etc/profile 执行命令(取决于用户选项)。这些 scipts 也重新定义了 PS1 var。所以我的
setenv("PS1", newPS1.c_str());
没有效果。
有什么建议吗?