# Help with cmd while using ps

**URL:** <https://community.unix.com/t/help-with-cmd-while-using-ps/353463>\
**Category:** Shell Programming and Scripting\
**Created:** [June 19, 2015, 2:20am UTC](https://community.unix.com/t/help-with-cmd-while-using-ps/353463 "2015-06-19T02:20:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jojo90](https://community.unix.com/letter_avatar/jojo90/32/6_449c533f586939b6e5f246632a46421f.png) [@Jojo90](https://community.unix.com/u/Jojo90)\
**Post date:** [June 19, 2015, 2:20am UTC](https://community.unix.com/t/help-with-cmd-while-using-ps/353463/1 "2015-06-19T02:20:20Z")

</div>

Hi i am new to shell scripting and any help is really appreciated.  
my requirement is in,

```nohighlight
ps -e -o pid,uname,cmd 

```

how can i split and take only the cmd part from it.  
I tried awk but the issue is when the cmd is returning a lengthy command which itself has some spaces it is truncating the value.for example,

```nohighlight
 ps -e -o pid,uname,cmd

```

```
 returns below

```

```nohighlight
 2001 root /sbin/mingetty /dev/tty6
 2002 root /sbin/udevd -d
 2003 root -bash
 2004 root /sbin/dhclient -1 -q -lf /var/lib/dhclient/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0

```

when using awk in ps to fetch only cmd it is truncating value as below,

```nohighlight
 ps -e -o pid,uname,cmd | awk '{ print $3 }'  

```

```nohighlight
 /sbin/mingetty
 /sbin/udevd
 -bash
 /sbin/dhclient

```

Expected value is

```nohighlight
 /sbin/mingetty /dev/tty6
 /sbin/udevd -d
 -bash
 /sbin/dhclient -1 -q -lf /var/lib/dhclient/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0

```

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [June 19, 2015, 2:32am UTC](https://community.unix.com/t/help-with-cmd-while-using-ps/353463/2 "2015-06-19T02:32:23Z")

</div>

```nohighlight
ps -e -o cmd

```

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [June 19, 2015, 11:46am UTC](https://community.unix.com/t/help-with-cmd-while-using-ps/353463/3 "2015-06-19T11:46:07Z")

</div>

> [@jojo90](#):
>
> ```plaintext
> ps -e -o pid,uname,cmd 
> 
> ```
> 
> how can i split and take only the cmd part from it.

You can of course specifiy only the "cmd"-part in the option-string of `ps` like Aia has already suggested. If you need all three values you can split these in the shell:

```plaintext
ps -e -o pid,uname,cmd |\
while read PID UNAME CMD ; do
     print - "PID=$PID , user=$UNAME , command=$CMD"
done

```

If you use a `read` command to fill several variables (three in this case) the input to fill them (one line from the output of `ps` ) will be split along word boundaries, so the first "word" goes to the first variable, the second word to the second variable. If there are more "words" in the output than there are variables the last variable gets the whole rest of the line.

So, this works because the first two values are always single "words" (character groups surrounded by blanks), only the last one isn't. It would not work if there would be several multi-word values involved.

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [June 19, 2015, 1:19pm UTC](https://community.unix.com/t/help-with-cmd-while-using-ps/353463/5 "2015-06-19T13:19:39Z")

</div>

Tip1:  
If you want this portable to Unix systems, stick to Posix.  
The "Man Pages" at the top of the [unix.com](http://unix.com) pages guide you to the [Posix man page for ps](http://www.unix.com/man-page/posix/1p/ps/).

```plaintext
ps -e -o pid,user,args |
while read PID USER ARGS ; do
  printf "%s\n" "PID=$PID , user=$USER , command='$ARGS'"
done

```

Also the ksh/zsh-only `print` was replaced by the portable `printf` .  
Tip 2:  
If you do not want a header line then use `ps -e -o pid= -o user= -o args=` .

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [June 19, 2015, 2:53pm UTC](https://community.unix.com/t/help-with-cmd-while-using-ps/353463/6 "2015-06-19T14:53:54Z")

</div>

You know what they say. "Assume makes an ass of you and me". Nevertheless, if someone is, for some estrange reason, fixated in the exercise of getting the desired output, disregarding the portion that was included on purpose on first place, the output can be manipulated to be more convenient for manipulation with awk, in this case.

Example: You can tell ps to use a coma as a separator, and then use that separator easily with awk.

```nohighlight
ps -eo "%U,%t,%a" | awk -F"," 'NR > 1 {print $3}'

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [June 19, 2015, 3:39pm UTC](https://community.unix.com/t/help-with-cmd-while-using-ps/353463/7 "2015-06-19T15:39:12Z")

</div>

And if the arguments contain commas??

```nohighlight
ps -eo "%U,%t,%a" | awk -F"," 'NR > 1 {print $3}'
...
ps -eo %U
...

```

😛

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [June 19, 2015, 3:46pm UTC](https://community.unix.com/t/help-with-cmd-while-using-ps/353463/8 "2015-06-19T15:46:24Z")

</div>

> [@madeingermany](#):
>
> And if the arguments contain commas??
> 
> ```plaintext
> ps -eo "%U,%t,%a" | awk -F"," 'NR > 1 {print $3}'
> ...
> ps -eo %U
> ...
> 
> ```
> 
> 😛

If the arguments contain commas blame on you because you assumed. Cry, feel miserably for a while and learn to not do it again.  
😛

---

<div class="post-metadata">

**Author:** ![Jojo90](https://community.unix.com/letter_avatar/jojo90/32/6_449c533f586939b6e5f246632a46421f.png) [@Jojo90](https://community.unix.com/u/Jojo90)\
**Post date:** [June 24, 2015, 4:27pm UTC](https://community.unix.com/t/help-with-cmd-while-using-ps/353463/11 "2015-06-24T16:27:02Z")

</div>

Thanks everyone for the reply.These where really helpful.
