Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sourcing a bash script from QT application



  • I am new to Qt. I want to run a bash script that exports some environmental variables then using the exported variables in my qt application . I am able to get environmental variables using QprocessEnvironment. The problem is I can't source the script and running using Qprocess::execute or Qprocess::startdetached() does not export the variables



  • @ahtarek
    This has nothing to do with Qt. You cannot run a subprocess in Linux which affects the environment variables in the program which executes it. source is a builtin command in bash which executes in the bash and does not create any subprocess.

    In order to change the values of environment variables in (say) a Qt program you need to change the environment variables in that program (e.g. Qt has qputenv()). You might read the lines from a (script) file, but you would have to "parse" it so that you can make calls to update the environment from within the program.

    This does assume you want the environment changed in the calling Qt program itself. If you only want to have those variables so that you can QProcess something else, you might consider instead executing a command which sources them followed by executing the command, like

    process.start("/bin/bash", { "-c", "source script && run some command" });
    

Log in to reply