- Linux / Unix Shell Script: Get Current User Name
- Syntax
- Shell script example
- A note about $EUID
- How to determine the current user account in Linux
- Summing up
- How do I find out what shell I am using on Linux/Unix?
- How can I find out what shell I am using?
- How do I check which shell am I using?
- How do I check how many shells are installed on my Linux box?
- Okay, so when I open the Terminal app, which shell is opened by default?
- How to check which shell am I using:
- Conclusion
- Linux List All Users In The System Command
- Linux list all users account using the /etc/passwd file
- How to list users in Linux using pagers
- Linux list user names only
- Get a list of all users using the getent command
- Find out whether a user account exists in the Linux server
- How to count user accounts in the Linux server
- A Note About System and General Users
- 3 Ways to Change a Users Default Shell in Linux
- 1. usermod Utility
- 2. chsh Utility
- 3. Change User Shell in /etc/passwd File
- If You Appreciate What We Do Here On TecMint, You Should Consider:
Linux / Unix Shell Script: Get Current User Name
I am working on a shell script. I need to find out the current user name. How do I find out the current user name under Bash or Ksh shell running on Linux or Unix like operating systems? How do I determine the current user account in Linux?
You can use the variables $USER, or $USERNAME which are not Bash builtins. These are, however, set as environmental variables in one of the Bash startup files. You can use the id command to get the same information.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Bash/ksh |
Est. reading time | 1m |
a] $USER – Current user name.
b] $USERNAME – Current user name.
c] id command – Print current user name.
d] whoami command – Show current user name.
Syntax
To get the current user name, type:
Get the current user name and store to a shell variable called $u:
The syntax for id command is:
To print numeric UID, run:
The following script reads user name and store to a variable called _user _uid:
Shell script example
Make sure only root user can run the following script:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
A note about $EUID
This variable EUID is readonly. It expands to the effective user ID of the current user, initialized at shell startup. You can use $EUID to find out if user is root or not with the following syntax:
How to determine the current user account in Linux
We can use the who command as follows to print the current user account:
who
whoami
OR use the id command:
id -u -n
Summing up
We explained how you could find and get the current user’s username in a Bash script and shell prompt using various command-line options. See bash man page for more information by typing the following commands:
man bash
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How do I find out what shell I am using on Linux/Unix?
B oth Linux and Unix provides various shell out of the box. One can find bash (Bourne Again shell), ksh (Korn shell), csh (C shell)/tcsh (TC shell), sh (Bourne shell) and more installed by default. However, how do you check which shell am I using? What is the best way to find out what shell I am using on Linux? The echo $SHELL is not so reliable. This page explains how to find out which shell I am using at a Linux, MacOS, FreeBSD, or Unix-like systems.
How can I find out what shell I am using?
The following echo command or printf command should work:
echo «$SHELL»
OR
printf «My current shell — %s\n» «$SHELL»
Please note that $SHELL is the shell for the current user but not necessarily the shell that is running at the moment. Try the following examples
How do I check which shell am I using?
Here is another old good Unix trick. Use the ps command with -p
ps -p $$
Sample outputs:
So what is a $ argument passed to the -p option? Remember $ returns the PID (process identification number) of the current process, and the current process is your shell. So running a ps on that number displays a process status listing of your shell. In that listing, you will find the name of your shell (look for CMD column).
ps -p $$
Sample outputs:
From my Linux box:
ps -p $$
Sample outputs:
You can store your shell name in a variable as follows :
MYSHELL=`ps -hp $$|awk ‘
Please note those are backquotes, not apostrophes. Or better try out the following if you have a bash shell:
MYSHELL=$(ps -hp $$|awk ‘
Another option is as follows:
echo $0
OR
printf «%s\n» $0
Sample outputs from the above commands:
Fig.01: Linux check which shell am I using
How do I check how many shells are installed on my Linux box?
The /etc/shells is a text file which contains the full pathnames of valid login shells. Type the following [nixmd name=”cat”] to see list how many shells are installed on your Linux or Unix box:
cat /etc/shells
Use /etc/shells file to check how many shells are installed on your system
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Okay, so when I open the Terminal app, which shell is opened by default?
Your default shell is defined in /etc/passwd file. So try the following grep command:
How to check which shell am I using:
Use the following Linux or Unix commands:
- ps -p $$ – Display your current shell name reliably.
- echo «$SHELL» – Print the shell for the current user but not necessarily the shell that is running at the movement.
- echo $0 – Another reliable and simple method to get the current shell interpreter name on Linux or Unix-like systems.
- readlink /proc/$$/exe – Another option to get the current shell name reliably on Linux operating systems.
- cat /etc/shells – List pathnames of valid login shells currently installed
- grep «^$USER» /etc/passwd – Print the default shell name. The default shell runs when you open a terminal window.
- chsh -s /bin/ksh – Change the shell used from /bin/bash (default) to /bin/ksh for your account
Conclusion
Sometimes things are not easy as they seem, and this page is the perfect example of it. I hope you found the suggestion useful when it comes to checking your current running shell. Bash users can display shell version by typing the following command:
$ bash —version
Here is what I got from my Ubuntu Linux 20.04 LTS desktop:
Источник
Linux List All Users In The System Command
Tutorial requirements | |
---|---|
Requirements | Linux |
Root privileges | No |
Difficulty | Easy |
Est. reading time | 5 mintues |