[Solved] How to get domain user name. User whoami

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ruyzz

[Solved] How to get domain user name. User whoami

Post by ruyzz »

With "Whoami" I get the user's name through the terminal Ctrl + Alt + T, but with bash it has root.

#! / bin / bash

usr = $ (whoami)


But through bash it gives me root and not the current username.

Please help me!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29588
Joined: Wed Jul 06, 2011 3:58 am

Re: How to get domain user name. User whoami

Post by xenopeek »

That means you're running your script with sudo. Is that needed?

In that case you can use the environment variable $SUDO_USER, it will have the name of the user that invoke sudo yourscript.sh. This variable is set by sudo.

Reliable way in bash is to test if $EUID equals 0 (zero). If so, your script is running as root. In that case use the $SUDO_USER variable for the username. Otherwise (user is running your script as themselves) you can use $USER variable.
Image
ruyzz

Re: How to get domain user name. User whoami

Post by ruyzz »

Thank you.

I have another problem. I want to access the desktop taking into account the current user, but it returns me:

domain \ user, when it should be: domain / user

The problem is the backslash.
Kyowash

Re: How to get domain user name. User whoami

Post by Kyowash »

ruyzz wrote: Mon Apr 30, 2018 6:26 pm Thank you.

I have another problem. I want to access the desktop taking into account the current user, but it returns me:

domain \ user, when it should be: domain / user

The problem is the backslash.
You can use sed:

Code: Select all

$ echo "user \ domain" | sed 's/\\/\//g'
user / domain
lmuserx4849

Re: How to get domain user name. User whoami

Post by lmuserx4849 »

Are environmental variables an option: $USER, $HOSTNAME

Type set|less to view.

Bash documentation: HOSTNAME
Login documentation: USER
ruyzz

Re: [Solved] How to get domain user name. User whoami

Post by ruyzz »

Thank you, xenopeek, Kyowash and lmuserx4849!
Locked

Return to “Scripts & Bash”