# execute\_process for detecting bash: issues with grep and awk commands arguments

**URL:** https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833
**Category:** Usage
**Created:** [April 5, 2023, 1:55pm UTC](https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833 "2023-04-05T13:55:56Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [April 5, 2023, 1:55pm UTC](https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833/1 "2023-04-05T13:55:56Z")

</div>

Hi,

I try to detect the running shell with the following cmake command:

```cmake
execute_process(COMMAND ps
COMMAND grep `echo $$`
COMMAND awk `{ print $4 }`
OUTPUT_VARIABLE RUNNING_SHELL RESULT_VARIABLE ERROR_HAPPENED)

```

but I get:  
grep: $$`: No such file or directory with this type awk: command line:1: `{  
awk: command line:1: ^ invalid charactere " ` " within the expression

expected output (for instance)  
RESULT\_VARIABLE contains “bash”  
ERROR\_HAPPENED contains “0” or 0 (I don’t know exactly)

Besides how can I test properly ERROR\_HAPPENED. Two situations may happen:  
1- the commands run to the last one and return an error code, possibly not 0 if something wrong happened  
2- one command fails to run, leaving ERROR\_HAPPENED unset or empty

my goal is something like that

```cmake
IF (<An error happened>)
 <manage error>
ELSE()
 MESSAGE("Found shell is ${RUNNING_SHELL}")
 <process success>
ENDIF()
ENDIF()

```

in fine I’d like to run execute\_process within the INSTALL command in order to run additionnal commands during install, commands that will depend on the OS and the actual shell.

How can I fix my detection, without any prerequisite on the available shell(s)?

Regards,  
A.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [April 6, 2023, 10:10am UTC](https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833/2 "2023-04-06T10:10:40Z")

</div>

First `awk `{ print $4 }`` must be replaced by `awk '{ print $4 }'`  
Second I could launch my command by delegating it to a script that I call from execute\_process but then, it returns me the wrong shell.  
The issue is on my part as cmake is launch through a bunch of scripts and the actual calling shell is changed on the way.  
Thus ps | grep echo$$ is not the way to go…  
My ultimate goal is to update PATH variable automatically during installation process but I’m starting to wonder if it’s actually possible under linux, as, in my undertanding, this variable must be updated from shell source file will be different from a shell to another and then it should be set for all possible shell on the targeted machine…  
What would be the correct way to install an executable as a command, or giving path to a so library? At least only for a user (without the need to prompt for admin rights)

Thanks  
A.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [April 6, 2023, 10:20am UTC](https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833/3 "2023-04-06T10:20:59Z")

</div>

maybe directly editing ~/.profile?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 24, 2023, 4:24am UTC](https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833/4 "2023-04-24T04:24:42Z")

</div>

> [@adaldev](#):
>
> My ultimate goal is to update PATH variable automatically during installation process but I’m starting to wonder if it’s actually possible under linux

There’s no consistent way to make sure this works reliably. All kinds of different shells, setups, and whatnot are in effect in this area.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [April 24, 2023, 7:40am UTC](https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833/5 "2023-04-24T07:40:59Z")

</div>

Sad.

I’m running for echo $SHELL and editing the corresponding rc file. Thus I will support a limited set of shells.

Regards  
A.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 24, 2023, 11:32am UTC](https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833/6 "2023-04-24T11:32:48Z")

</div>

FWIW, editing people’s files like this should be documented and optional; I would certainly not be happy with programs editing my shell configs willy-nilly like that (because `PATH` is set up in `~/.local/share/systemd/user/path-env.service` on my machine and `zshrc` getting random stuff would be…unfortunate).

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [April 24, 2023, 12:30pm UTC](https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833/7 "2023-04-24T12:30:34Z")

</div>

don’t worry, I prompt the user for permission 😉 (see [using read -p from in script launched by execute\_process](https://discourse.cmake.org/t/using-read-p-from-in-script-launched-by-execute-process/7941))
