开发者

grep user name in unix

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.


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`


id -un

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜