Conditional Checks in Shell: Variable Usage

Introduction

Conditional checks are fundamental to shell scripting, enabling scripts to make decisions based on the values of variables. This section explores how variables are used in conditional checks, focusing on the interplay between variables and the logical operators that govern decision-making in shell scripts.

Variables in Shell

Variables in shell scripts store data, and their usage is critical for dynamic content handling. Key aspects include:

  • Types of variables:

    • Local variables (e.g., var) are scoped to the current context.
    • Global variables (e.g., export var) persist across script executions.
    • Special variables (e.g., $$, $_, $$) track process IDs, shell history, and previous commands.
    • Environment variables (e.g., PATH) are set by the system and influence script behavior.
  • Scope and lifetime:
    Variables declared within a script block are local to that block, while those declared at the top level are global.

  • Case sensitivity:
    Shell scripts treat variables case-insensitively, but the value of a variable is case-sensitive. For example, VAR and var are treated as distinct.

  • Quoting and expansion:
    Quoting (e.g., "$var") prevents unintended expansion, while unquoted variables are expanded automatically.

Conditional Checks in Shell

Conditional checks in shell scripts are performed using if statements and test commands. The core syntax involves evaluating expressions and using logical operators to control flow.

Basic Conditional Syntax

Conditional checks use if followed by a condition and then/else blocks. For example:

if [ "$var" -eq 5 ]; then  
  echo "Value is 5"  
else  
  echo "Value is not 5"  
fi  

The [ ] syntax is preferred for clarity, while test commands (e.g., test -n "$var") offer more flexibility.

Logical Operators

Logical operators (&&, ||, &&, ||) allow chaining conditions. For example:

if [ "$var" -gt 10 ] && [ "$var" -lt 20 ]; then  
  echo "Value is between 10 and 20"  
elif [ "$var" -lt 10 ]; then  
  echo "Value is less than 10"  
else  
  echo "Value is greater than or equal to 20"  
fi  

These operators enable complex decision-making based on multiple conditions.

Test Commands

Test commands (e.g., test, [[ ]], `[[ -n "$var" ]]) evaluate expressions and return boolean results. For example:

if test -f "$file"; then  
  echo "File exists"  
else  
  echo "File does not exist"  
fi  

These commands are often used to check file types, sizes, or permissions.

Practical Examples

Checking for Empty Variables

To avoid errors, scripts often check if a variable is empty. For example:

if [ -z "$var" ]; then  
  echo "Variable is empty"  
else  
  echo "Variable is not empty"  
fi  

This ensures scripts do not fail when a variable is not defined.

Using Logical Operators

Logical operators can combine conditions. For example:

if [ "$var" -gt 10 ] || [ "$var" -lt 20 ]; then  
  echo "Value is within range"  
else  
  echo "Value is outside range"  
fi  

This allows scripts to handle multiple scenarios efficiently.

Handling Special Variables

Special variables like $$ (current process ID) and $_ (last command argument) can be used in conditions. For example:

if [ "$$$" -eq 123 ]; then  
  echo "Current process ID is 123"  
else  
  echo "Process ID is not 123"  
fi  

These variables provide runtime context for conditional checks.

Conclusion

Conditional checks in shell scripts rely on the proper use of variables and logical operators. By mastering variable handling and conditional syntax, developers can create robust, dynamic scripts that respond to runtime data. Key practices include:

  • Using quotes to prevent unintended expansion.
  • Leveraging special variables for context.
  • Employing logical operators to handle complex conditions.
  • Testing for empty variables to avoid errors.
    These principles ensure scripts are reliable and maintainable.