1

In linux if you type id www-data you get the id.

uid=33(www-data) gid=33(www-data) groups=33(www-data)

Is there a way to extract the value for the uid from this?

I think I could pipe it like this:

id www-data | grep ?

I could also use egrep or sed if its easier, but not sure how to proceed. Similar examples on Stackoverflow deal with the direct extraction of the value via regex. I don't know if the value would be 33. Hence I need to extract it from uid= until (www-data). (unless that is always 33, and I am making my life too difficult). Any suggestion?

1
  • 2
    How about id -u www-data? Feb 20, 2015 at 16:04

2 Answers 2

4

Instead of using a regex, you can use the flags of id (from man id):

-g, --group
       print only the effective group ID
-G, --groups
       print all group IDs
-n, --name
       print a name instead of a number, for -ugG
-r, --real
       print the real ID instead of the effective ID, with -ugG
-u, --user
       print only the effective user ID

This is probably less error prone: if in the future id would change the way it formats the output, the flag will still work.

So use:

$ id -u www-data
33

In general many of these commands offer flags to retrieve parts of the information. Consulting the manpage in general results in a lot of options and only occasionally you find you need something that is not implemented.

1
  • 1
    I changed the question to reflect the answer better,
    – Houman
    Feb 20, 2015 at 16:24
1

You could use grep,

id www-data | grep -oP 'uid=\K\S+'

OR

sed,

id www-data | sed -n 's/.*\buid=\([^ ]\+\).*/\1/p'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.