简体   繁体   English

如何用C++程序重新加载Linux中的.bash_profile?

[英]How to reload .bash_profile in Linux with C++ program?

I used system("source ~/.bash_profile") to reload it, but it does not work.我使用 system("source ~/.bash_profile") 重新加载它,但它不起作用。 Is there any other function or code to reload the bash with C++ program in linux? linux中是否还有其他function或代码用C++程序重新加载bash?

The command source ~/.bash_profile works, if you're actually in the process running the bash shell you want to affect.如果您实际上正在运行要影响的bash shell,则命令source ~/.bash_profile有效。

When you started your C++ program, that started up in a different process, a child of the bash process, and therefore unable to modify the environment of that bash process.当您启动 C++ 程序时,该程序在另一个进程中启动,它是bash进程的子进程,因此无法修改该bash进程的环境。

And, in fact, it's even worse than that since a system() call in a C++ program almost certainly runs another process to do that work, so it's twice removed from the bash process.而且,事实上,它甚至比这更糟糕,因为 C++ 程序中的system()调用几乎肯定会运行另一个进程来完成这项工作,因此它两次从bash进程中删除。

And, in fact, it's worse even than that, since source is a bash internal command, meaning you'd have to start up a bash shell to execute it:-)而且,事实上,它甚至比这更糟糕,因为source是一个bash内部命令,这意味着你必须启动一个bash shell 来执行它:-)

Hence, what you end up with is the following hierarchy of processes, all of which can really only affect their own environment, not anything above them:因此,您最终得到的是以下流程层次结构,所有这些实际上只能影响它们自己的环境,而不会影响它们之上的任何东西:

  original-bash
        |
   C++-program
        |
bash-run-by-system

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM