0

I want to grep the username. the command id gives the following.
id output is
uid=0(root) gid=0(root)

but i want the output as only root or who ever the user is.

2 Answers 2

1

That's not a grep, that's a cut:

$ id | cut -d '(' -f 2 | cut -d ')' -f 1

If you're doing this in a script (as a comment implies), you need to capture the output of the command properly. In bash, use:

USER=$(id | cut -d '(' -f 2 | cut -d ')' -f 1)

Many o(th|ld)er shells support the backtick syntax:

USER=`id | cut -d '(' -f 2 | cut -d ')' -f 1`
Sign up to request clarification or add additional context in comments.

1 Comment

when i use it in a script , it displays nothing #!/bin/bash Userid=id | cut -d '(' -f 2 | cut -d ')' -f 1 echo $Userid
0
id -un

will directly print the name associated with the current effective user-id.

1 Comment

it stays illegal option --u . im using solaris

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.