0

I'm trying to write a script to automate a task that involves the logged user's username. The problem is the username is coming up as domain/username and I just need the username.

LOGGEDUSER=$(stat -f "%S" /dev/console);

Any way to output the result and remove part of it? This currently outputs domain/username I need to remove the domain/.

  • Wouldn't that be stat -f "%Su" /dev/console ? – fd0 Mar 1 '18 at 17:04
1

Local Domain

See Get current users username in bash? for a range of approaches:

logname

or

echo $USER

or

whoami

or

id -u -n

Network Domain

If you are on an (Active Directory) network domain, you can use

echo ${USER#*\\}

or

u=$(logname)
echo ${u#\\}

to just get the user name part.

  • Thanks for your quick reply! Any of those methods output DOMAIN\username I need to be able to omit the DOMAIN\ part... – Webster Master Mar 1 '18 at 16:53
  • All of the above methods only output the username. – Graham Miln Mar 1 '18 at 17:09
  • Do you use a local or a network login? You can easily strip the domain/ part with bash functionality actually – nohillside Mar 1 '18 at 17:10
  • That's correct it's a network login that's why domain\ is being added to the username. How can I strip it? – Webster Master Mar 1 '18 at 17:16
  • 1
    @WebsterMaster See updated answer. – nohillside Mar 1 '18 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .