Usage of HOSTNAME and CURRENT_USER in scripts on linux
11,060
Solution 1
$USER
should typically be set to the current user, and $HOSTNAME
to the current hostname. If not, you can also get them from the output of whoami
and hostname
Also, the user's home directory is not necessarily in /home/[username]
. You should use the value of $HOME
instead.
Solution 2
If you wish to see all of your environmental variables available, use the following command:
bash$ set
Remember case sensitivity, for hostname you'll need $HOSTNAME, and $USER
bash$ echo $USER
sparks
bash$ echo $HOSTNAME
servername
Related videos on Youtube
Author by
Alex
Updated on September 17, 2022Comments
-
Alex over 1 year
Suppose I want to edit
/etc/profile
:PYTHONPATH = /home/CURRENT_USER/
- How do I have a variable that will automatically fill in the current user?
- What about hostname?
- Aren't these environment variables?
-
Kernel Stearns over 6 yearsOne thing to be careful with here - if you ever run this script as root by using
sudo
, then bothwhoami
and$USER
will returnroot
, regardless of what user ran the script.
-
Alex over 14 yearsWhy is it that when I type $HOSTNAME in my terminal command, it says: bash: /root: is a directory
-
EEAA over 14 yearsNo, you need to
echo $HOSTNAME
. If you just give bash a variable as a command, how is it supposed to know what action to take on it?