使用重新定义的 PS1 环境变量运行 bash
我编写了名为 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 中的命令(具体取决于关于用户选项)。这些脚本也重新定义了 PS1 var。所以我的
setenv("PS1", newPS1.c_str());
没有效果。
有什么建议吗?
I have written cpp aplication called MyShell that takes as the params some real shell name (generally bash) and its params. MyShell works as a wrapper for it.
I need to change command prompting for the inner shell, specifically the PS1 environmental variable.
I know how to do it command-line way using PS1 env var:
$ PS1="[myshell]"$PS1
[myshell]$
But it is not so easy to do that from cpp application:
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, when bash is invoked it executes command from /etc/.bashrc or /etc/profile (depending on users options). Those scipts redefine PS1 var too. So my
setenv("PS1", newPS1.c_str());
has no effect.
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想更改默认设置的提示符,可以将
export PS1="[myshell]"$PS1
附加到~/.bashrc
或~/.在启动 shell 之前从您的 cpp 应用程序中获取 profile
,并在完成后抑制它。编辑
如果您不想更改原始
~/.bashrc
文件,您可以 invoque:with
/tmp/myCustomPS1
包含:If you want to change only prompt from default settings, you can append
export PS1="[myshell]"$PS1
to~/.bashrc
or~/.profile
from your cpp application before launching your shell and suppress it after completion.EDIT
If you don't want to alter original
~/.bashrc
file you can invoque:with
/tmp/myCustomPS1
containing:一旦 bash 的子进程(子进程)被调用,它就可以随心所欲地处理其环境。这包括用其他值替换 PS1 的值。毕竟,它只是一个环境变量。
父进程不能强制子进程保留某些环境变量。父进程可以传递某些环境变量,但仅此而已。
您可以使用 PROMPT_COMMAND 等执行其他操作,但所有这些都可以被子进程覆盖。
如果您希望子进程强制执行有关环境变量的某些行为,则必须修改该程序以添加您想要的行为。
然后您将拥有自己的自定义提示。也许甚至应该将您在 MyShell 中执行的任何其他操作都放入其中并完成它。
Once the sub process (child) of bash is invoked, it's free to do with its environment what ever it wants. This includes replacing your values for PS1 with something else. After all, it's just an environment variable.
The parent process cannot force the child process to keep certain environment variables. The parent process can pass certain environment variables, but that's it.
You can do other things with PROMPT_COMMAND and so on, but all of these can be overridden by the child process.
If you want the child process to enforce certain behavior with respect to environment variables, you'll have to modify that program to add the behavior you want.
Then you'll have your own custom prompt. Probably even should roll whatever else you do in MyShell into this and be done with it.
您可以使用命令 --norc 停止 bash 读取 .bahsrc 文件,并使用 --noprofile 停止读取配置文件,
例如
You can stop bash reading the .bahsrc files by using the command --norc and the profiles by --noprofile
e.g.