Get current users username in bash?

天涯浪子 提交于 2019-12-02 13:54:40
SethMMorton

On the command line enter

echo "$USER"

or

whoami

To save these values to a variable, do

myvariable=$(whoami)

or

myvariable=$USER

OF course, you don't need to make a variable since that is what the $USER variable is for.

alternative to whoami is id -u -n.

id -u will return the user id (e.g. 0 for root).

A hack the I've used on Solaris 9 and Linux and which works fine for both of them:

ps -o user= -p $$ | awk '{print $1}'

This snippet prints name of user with current EUID.

NOTE: you need bash as interpreter here.

On Solaris you have problems with methods, described above:

  • id does not accept -u and -n parameters (so you will have to parse output)
  • whoami does not exist (by default)
  • who am I prints owner of current terminal (ignores EUID)
  • $USER variable is set correctly only after reading profile files (e.g. /etc/profile)

Use the standard Unix/Linux/BSD/MacOS command logname to retrieve the logged in user. This ignores the environment as well as sudo, as these are unreliable reporters. It will always print the logged in user's name and then exit. This command has been around since about 1981.

My-Mac:~ devin$ logname
devin
My-Mac:~ devin$ sudo logname
Password:
devin
My-Mac:~ devin$ sudo su -
My-Mac:~ root# logname
devin
My-Mac:~ root# echo $USER
root
Sireesh Yarlagadda

Two commands:

  1. id prints the user id along with the groups. Format: uid=usernumber(username) ...

  2. whoami gives the current user name

OldManRiver

When root (sudo) permissions are required, which is usually 90%+ when using scripts, the above methods always give you root as the answer.

To get the current "logged in" user is just as simple, but requires accessing different variables: $SUDO_UID and $SUDO_USER.

They can be echoed:

echo $SUDO_UID
echo $SUDO_USER

or assigned, e.g.:

myuid=$SUDO_UID
myuname=$SUDO_USER
Fred Mitchell

For bash/ksh/sh etc. Many of your questions are quickly answered by either:

man [function]

to get the documentation for the system you are using.

or usually more conveniently: google "man function" This may give different results for some things where linux and unix have modest differences.

For this question, just enter "whoami" in your shell.

To script it:

myvar=$(whoami)

Lin-man

In Solaris OS- I used this command.

$ who am i     # remember to use it with space.

On Linux- Someone already answered this in comments.

$ whoami       # without space

On Most Linux systems, simply typing whoami on the command line provides the user ID.

However, on Solaris, you may have to determine the user ID, by determining the UID of the user logged-in through the command below.

echo $UID

Once the UID is known, find the user by matching the UID against the /etc/passwd.

cat /etc/passwd | cut -d":" -f1,3

The current user's username can be gotten in pure Bash with the ${parameter@operator} parameter expansion (introduced in Bash 4.4):

$ : \\u
$ printf '%s\n' "${_@P}"

The : built-in (synonym of true) is used instead of a temporary variable by setting the last argument, which is stored in $_. We then expand it (\u) as if it were a prompt string with the P operator.

This is better than using $USER, as $USER is just a regular environmental variable; it can be modified, unset, etc. Even if it isn't intentionally tampered with, a common case where it's still incorrect is when the user is switched without starting a login shell (su's default).

leesagacious

Get the current task's user_struct

#define get_current_user()              \
({                                      \
    struct user_struct *__u;            \
    const struct cred *__cred;          \
    __cred = current_cred();            \
    __u = get_uid(__cred->user);        \
    __u;                                \
})
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!