Variables .

Variables in Linux Shell Scripting ?

In Linux shell scripting, variables are used to store data that can be referenced and manipulated throughout the script. Variables make scripts more dynamic and flexible. Let's explore how to use variables in shell scripting.

Defining and Using Variables

1. Defining Variables: Variables are defined by simply assigning a value to a name. There should be no spaces around the = sign.

bash

variable_name="value"

2. Using Variables: To reference the value stored in a variable, precede the variable name with a $ sign.

echo $variable_name

Example: Basic Usage of Variables

Here's a simple example demonstrating how to define and use variables in a shell script:


#!/bin/bash
# Defining variables
greeting="Hello"
name="Alice"
# Using variables
echo "$greeting, $name!"


Types of Variables

1. User-Defined Variables: Variables defined by the user within a script.

# Define a user variable
my_var="This is a user-defined variable"
echo $my_var


2. Environment Variables: Predefined variables available in the shell environment. Common examples include HOME, PATH, and USER.

# Use an environment variable
echo "Home directory: $HOME"
echo "Current user: $USER"


3. Positional Parameters: Variables that hold the values of command-line arguments passed to a script.

# Access positional parameters
echo "First argument: $1"
echo "Second argument: $2"
Example: Using Positional Parameters

Here's an example script that uses positional parameters:

When we execute the following script with " ./script_name " . we need to pass the command line arguments as follows ,

   "  ./script_name  Hello World "

Then the first argument "Hello" will be passed to $1 and second argument "World" will be passed to $2.

and will be printed to computer screen with echo command.

#!/bin/bash
# Positional parameters
first_arg=$1
second_arg=$2

# Using positional parameters
echo "First argument: $first_arg"
echo "Second argument: $second_arg"


Special Variables

1. $0: The name of the script.

2. $#: The number of arguments passed to the script.

3. $@: All arguments passed to the script.

4. $?: The exit status of the last command executed.

5. $$: The process ID of the current shell.

6. $(command): Command substitution, used to capture the output of a command.

# Command substitution
current_date=$(date)
echo "Current date and time: $current_date"
Example: Using Special Variables

Here's an example script demonstrating the use of special variables:


#!/bin/bash
# Special variables
script_name=$0
arg_count=$#
all_args=$@
exit_status=$?
process_id=$$

# Displaying special variables
echo "Script name: $script_name"
echo "Number of arguments: $arg_count"
echo "All arguments: $all_args"
echo "Exit status of last command: $exit_status"
echo "Process ID of current shell: $process_id"


Variables in shell scripting are powerful tools that enhance the functionality and flexibility of your scripts. They allow you to create more dynamic and adaptable scripts to handle a wide range of tasks.