我想grep用户名。
命令id给出以下内容
id输出为
uid = 0(root)gid = 0(root)
但我希望输出只作为root用户或者用户是谁。
答案 0 :(得分:1)
这不是一个grep,那是一个cut:
$ id | cut -d '(' -f 2 | cut -d ')' -f 1
如果您在脚本中执行此操作(如注释所示),则需要正确捕获命令的输出。在bash中,使用:
USER=$(id | cut -d '(' -f 2 | cut -d ')' -f 1)
许多o(th | ld)shell支持反引号语法:
USER=`id | cut -d '(' -f 2 | cut -d ')' -f 1`
答案 1 :(得分:0)
id -un
将直接打印与当前有效用户ID关联的名称。