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
Share:
11,060

Related videos on Youtube

Alex
Author by

Alex

Updated on September 17, 2022

Comments

  • Alex
    Alex over 1 year

    Suppose I want to edit /etc/profile:

    PYTHONPATH = /home/CURRENT_USER/

    1. How do I have a variable that will automatically fill in the current user?
    2. What about hostname?
    3. Aren't these environment variables?
    • Kernel Stearns
      Kernel Stearns over 6 years
      One thing to be careful with here - if you ever run this script as root by using sudo, then both whoami and $USER will return root, regardless of what user ran the script.
  • Alex
    Alex over 14 years
    Why is it that when I type $HOSTNAME in my terminal command, it says: bash: /root: is a directory
  • EEAA
    EEAA over 14 years
    No, 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?