# Run bash script in current process

**URL:** https://discourse.cmake.org/t/run-bash-script-in-current-process/10363
**Category:** Usage
**Created:** [March 14, 2024, 12:30pm UTC](https://discourse.cmake.org/t/run-bash-script-in-current-process/10363 "2024-03-14T12:30:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [March 14, 2024, 12:30pm UTC](https://discourse.cmake.org/t/run-bash-script-in-current-process/10363/1 "2024-03-14T12:30:15Z")

</div>

This is a long shot, but I’d like to check that I haven’t missed something in the documentation before I embark on a complicated workaround.

I’m using a third-party library which, when using it in Linux, requires a bash script to be run that configures various environment variables which need to be set when compilation and linking take place.

I thought this would be simple: I created a very small CMake script, let’s call it RunScript.cmake, containing just:

```auto
execute_process(COMMAND script.sh)
message("The script has been run")

```

…and then used add\_custom\_target to ensure my script was run at build time:

```auto
add_custom_target(runLibraryScript
                  COMMAND ${CMAKE_COMMAND} -P RunScript.cmake
                  COMMENT "Running initialisation script")

```

Having made ‘runLibraryScript’ a dependency of the first target to be built, the script runs nicely. Then, needless to say, compilation fails because execute\_process didn’t run the script in the _current_ process, but in a child process: so all the environment variables created by the script were immediately lost when control returned to the current process.

Is there _any_ way for CMake to run a shell script in the same process as it’s going to use for the build?

Otherwise, I’m faced with having to embed my shell script in a second shell script which which run the first script in its own process, then output all its environment variable to a file, then get CMake (still at build time) to parse the file, extract the environment variables, and re-set them in the build process. That looks like more than the work of an afternoon.

So is there a simpler way that I’ve missed?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 14, 2024, 9:42pm UTC](https://discourse.cmake.org/t/run-bash-script-in-current-process/10363/2 "2024-03-14T21:42:37Z")

</div>

You can’t have a script modify the environment of its parent process.

CMake expects you to run both `cmake` and the build tool in a shell where the environment is already fully prepared. If you are running a script of some kind in a build step, you can incorporate logic in that script to modify _that script’s_ environment, but not the environment of the whole build process.

---

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [March 14, 2024, 10:40pm UTC](https://discourse.cmake.org/t/run-bash-script-in-current-process/10363/3 "2024-03-14T22:40:47Z")

</div>

Yes, I had a nasty feeling that that was the case. I suppose what I was hoping for was some equivalent of execute\_process that ‘sourced’ the script instead of running it.

Never mind; I’ll have to think of something else.

Thank you.
