Bash System Defined Variables

To know the list of these variables in your system, type the commands set, env, and printenv on the command line terminal as follows:

printenv        # shows all environment variables
env             # same as above
set             # shows all shell + user-defined variables

================================================

prabhumanyam@PrabhuPC:~$ cat sysvariables.sh
#!/bin/bash
# ---------------------------------------------
# Script Name: system_vars.sh
# Purpose: Display common system-defined variables
# ---------------------------------------------

echo "===== SYSTEM-DEFINED VARIABLES ====="
echo "User Name          : $USER"
echo "Home Directory     : $HOME"
echo "Current Shell      : $SHELL"
echo "Current Directory  : $PWD"
echo "Previous Directory : $OLDPWD"
echo "Hostname           : $HOSTNAME"
echo "User ID (UID)      : $UID"
echo "Effective UID      : $EUID"
echo "Path Variable      : $PATH"
echo "Terminal Type      : $TERM"
echo "Process ID (PID)   : $$"
echo "Script Name        : $0"
echo "Number of Arguments: $#"
echo "All Arguments (*)  : $*"
echo "All Arguments (@)  : $@"
echo "Last Command Status: $?"
echo "Random Number      : $RANDOM"
echo "Last Background PID: $!"
echo "Line Number        : $LINENO"
echo "Seconds Since Start: $SECONDS"
echo "today is";date
echo "no of users logged in"; who | wc -l
echo "calender"; cal
echo "---------------------------------------------"

prabhumanyam@PrabhuPC:~$ sh sysvariables.sh sandeep pr hari manyam 100 200

===== SYSTEM-DEFINED VARIABLES =====
User Name          : prabhumanyam
Home Directory     : /home/prabhumanyam
Current Shell      : /bin/bash
Current Directory  : /home/prabhumanyam
Previous Directory :
Hostname           :
User ID (UID)      :
Effective UID      :
Path Variable      : /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/Common Files/Oracle/Java/javapath:/mnt
Terminal Type      : xterm-256color
Process ID (PID)   : 5669
Script Name        : sysvariables.sh
Number of Arguments: 6
All Arguments (*)  : sandeep pr hari manyam 100 200
All Arguments (@)  : sandeep pr hari manyam 100 200
Last Command Status: 0
Random Number      :
Last Background PID:
Line Number        :
Seconds Since Start:
today is
Thu Nov 13 08:06:08 UTC 2025
no of users logged in
1
calender
   November 2025
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30


================================================
VariableDescriptionExample Output
$HOMEHome directory of the current user/home/prabhu
$USERUsername of the logged-in userprabhu
$PATHList of directories the shell searches for executables/usr/local/bin:/usr/bin:/bin:/usr/sbin
$PWDCurrent working directory/home/prabhu/scripts
$OLDPWDPrevious working directory/home/prabhu
$SHELLPath of the user’s login shell/bin/bash
$UIDUser ID number of the current user1000
$EUIDEffective User ID (useful when running with sudo)0 for root
$0Name of the current script./myscript.sh
$#Number of command-line arguments passed to the script3
$*All command-line arguments as a single stringarg1 arg2 arg3
$@All command-line arguments as separate stringsarg1 arg2 arg3
$$Process ID (PID) of the current shell/script5472
$?Exit status of the last executed command (0 = success)0
$!PID of the last background process5501
$HOSTNAMEName of the machinePrabhuPC
$RANDOMGenerates a random number each time it’s referenced26485
$LINENOCurrent line number in the script12
$SECONDSNumber of seconds the shell has been running45