Shell Variable Types: Environment Variables versus Regular Variables
Shell Script Fundamentals
Script Interpreter
The interpreter declaration must appear on the first line, starting with a shebang (#!):
#!/bin/bash
Different languages require different interpreters. The shebang tells the system which program to use for executing the script.
Comments
Comment lines use the hash symbol (#) anywhere except the first line. Comments can occupy full lines or appear after commands:
# This is a full-line comment
echo "Hello" # Inline comment
Avoid using non-ASCII characters in comments for portability.
Script Execution Methods
bash script.sh # Executes without requiring execute permissions
./script.sh # Requires execute permission on the file
source script.sh # Runs in current shell without spawning a subprocess
The first two methods spawn a child process—variables defined in child processes do not propagate to the parent shell. The source command executes the script within the current shell environment.
Shell Script Coding Standards
- Declare the interpreter on the first line
- Include version and copyright information at the top
- Avoid non-ASCII characters in scripts
- Use the
.shextension for script files - Store scripts in designated dircetories
- Write paired symbols (quotes, brackets) completely before filling content
- Ensure spaces around brackets:
[ condition ] - Complete control structure syntax before adding logic
- Use indentation consistently for readability
Quotation Usage
| Quote Type | Purpose |
|---|---|
| Double quotes | Standard string definitions with variable interpolation |
| Single quotes | Literal interpretation—content displays exactly as written |
| Backticks | Command substitution (legacy syntax) |
Alternative for command substitution:
$(command) # Preferred over backticks
All quotation marks must be ASCII characters. Avoid spaces around the assignment operator (=).
Environment Variables
Environment variables are system-wide variables exported via the export builtin. They persist in /etc/profile, /etc/bashrc, and user-specific files like ~/.bash_profile and ~/.bashrc. Custom environment variables follow the convention of uppercase naming.
Viewing Variables
| Command | Description |
|---|---|
set |
Displays all variables (global and local) |
env |
Shows only global environment variables |
declare |
Lists variables, functions, integers, and exported items |
set -o |
Displays bash configuration options |
Querying specific variables:
echo $PWD
# Output: /home/user
printf "%s\n" $PWD
# Output: /home/user
Removing Variables
unset VARIABLE_NAME
Defining Environment Variables
Three equivalent approaches:
export USERNAME="devuser"
declare -x USERNAME="devuser"
USERNAME="devuser"; export USERNAME
Persistence Configuration
User-level configuration (recommended):
~/.bashrc
~/.bash_profile
System-level configuration:
/etc/profile
/etc/bashrc # Recommended for system-wide settings
/etc/profile.d/
Place scripts in /etc/profile.d/ to execute automatically during login initialization.
Variable Resolution Order
Login shells:
/etc/profile → /etc/profile.d/ → ~/.bash_profile → ~/.bashrc → /etc/bashrc
Non-login shells:
~/.bashrc → /etc/bashrc
To ensure variables are available in non-login shells, write them to the later files in this sequence.
Login Shell Customization
/etc/motd # Static greeting message
/etc/profile.d/welcome.sh # Executable script with dynamic content
Java Environment Configuration
export JAVA_HOME=/opt/jdk-11
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
export RESIN_HOME=/opt/resin
Typically placed in /etc/profile. Scripts requiring Java should redefine these variables to ensure availability in child processes.
Common System Variables
| Variable | Purpose |
|---|---|
$HOME |
User's home directory on login |
$UID |
Current user's numeric identifier |
$PWD |
Current working directory |
$SHELL |
Default shell interpreter |
$USER |
Current username |
Regular (Local) Variables
Local variables exist only within the current shell session. When a script spawns a subprocess or the shell exits, these variables become inaccessible.
Naming Conventions
Variable names must begin with a letter or underscore, followed by alphanumeric characters or underscores. Quotes are optional for simple assignments but required when content includes spaces or special characters.
var1='simple_text'
var2="string with spaces"
var3=$(pwd)
var4=12345
Best Practices for Variable Assignment
- Unquoted assignment works for continuous alphanumeric strings
- Double quotes are necessary for complex content or non-sequential strings
- Single quotes preserve literal content exactly
- Command substitution requires backticks or
$()syntax - Choose descriptive names following camelCase or snake_case conventions
- Prefix the variable name with
$when referencing its value - Use curly braces to delimit variable names adjacent to other characters:
echo "The value is ${var1}suffix"
Cron Job Considerations
Scheduled tasks run in minimal environments. Scripts executed by cron should explicitly define required environment variables rather than relying on shell initialization.
Variable Expansion in Strings
Double quotes allow variable interpolation:
username="developer"
echo "Hello, $username" # Outputs: Hello, developer
Single quotes prevent interpolation:
echo 'Hello, $username' # Outputs: Hello, $username