All Bash Environment Variables Explained

x32x01
  • by x32x01 ||
In Linux, a shell like Bash is a program that runs operating system commands. When you start Bash, it automatically creates over 50 predefined variables. These variables provide key details about your current session and help control how the shell behaves.

For example, some variables store system information, others control how commands are executed, and a few can even generate random numbers or track process IDs.

⚠️ Important tip: Never delete or recreate predefined variables with the same names, as you might lose their special properties. For instance, $RANDOM gives you a random number every time - it won’t work if you unset it and recreate it manually.



🧠 How Environment Variables Work​

Think of Linux environment variables like layers of an onion 🧅.
Each program can pass its environment variables to another program it runs - but only if you export them first.

This allows Linux to share settings and session details between programs, making automation, scripting, and configuration incredibly powerful.



💡 Most Common Bash Environment Variables​

Here’s a categorized list of the most useful and commonly used Bash variables 👇

🔤 Command & Script Information​

  • $* - All command-line parameters as one string.
  • $@ - All command-line parameters as separate strings.
  • $# - Number of parameters passed.
  • $? - Exit status of the last executed command.
  • $$ - Process ID (PID) of the current shell.
  • $0 - The name of the current script or command.



⚙️ Bash Configuration Variables​

  • BASH - The full path to the Bash binary.
  • BASH_VERSION - The current Bash version.
  • BASH_VERSINFO - Detailed array of Bash version numbers.
  • BASH_ENV - Defines a startup file for bash scripts.
  • SHELLOPTS - A list of enabled Bash options.



🧮 User & System Information​

  • USER / LOGNAME - The current user name.
  • UID / EUID - The user’s numeric ID and effective ID.
  • HOSTNAME - The system’s host name.
  • OSTYPE - The operating system type.
  • MACHTYPE - CPU and machine type.



📂 Directory & Path Variables​

  • PWD - Current working directory.
  • OLDPWD - Previous working directory.
  • HOME - The user’s home directory.
  • PATH - A list of directories to search for executable files.



🕒 Time, Randomness, and History​

  • SECONDS - Number of seconds since the shell started.
  • RANDOM - Generates a random number each time it’s used.
  • HISTFILE - File used to save the command history.
  • HISTSIZE - Number of commands stored in history.
  • TMOUT - Timeout for read or select commands.



🖥️ Terminal & Display Variables​

  • LINES - Number of lines in the terminal.
  • COLUMNS - Number of columns in the terminal.
  • PS1 - The main command prompt format.
  • PS2, PS3, PS4 - Secondary and debugging prompts.



🧩 Why Bash Environment Variables Matter​

Understanding these variables is key to mastering Linux automation, scripting, and system management.
You can use them to:
  • Customize your prompt or shell behavior 🎨
  • Write dynamic scripts that adapt to the system 🌍
  • Control program execution and handle processes efficiently ⚡



🚀 Final Thoughts​

Bash environment variables are the backbone of shell scripting in Linux. They store everything from user info to runtime data and command history.
Once you understand them, you’ll gain full control over your Linux environment like a pro 💪🐧
 
Last edited:
Related Threads
x32x01
Replies
0
Views
744
x32x01
x32x01
x32x01
Replies
0
Views
821
x32x01
x32x01
x32x01
Replies
0
Views
749
x32x01
x32x01
x32x01
Replies
0
Views
799
x32x01
x32x01
x32x01
Replies
0
Views
2K
x32x01
x32x01
x32x01
Replies
0
Views
784
x32x01
x32x01
x32x01
Replies
0
Views
731
x32x01
x32x01
x32x01
Replies
0
Views
793
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
768
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
845
x32x01
x32x01
Register & Login Faster
Forgot your password?
Forum Statistics
Threads
628
Messages
632
Members
64
Latest Member
alialguelmi
Back
Top