Special Variables in Shell Scripting

LinuxLinuxBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn about the special variables in shell scripting. These variables provide important information about the script execution environment, such as command-line arguments, script name, and process ID.

Understanding the Special Variables

The special variables in shell scripting provide valuable information and can be accessed and used within your script. Here is a brief explanation of each special variable:

  • $0: Represents the filename of the current script.
  • $n: Represents the Nth argument passed to the script or function (where n is a number).
  • $#: Represents the number of arguments passed to the script or function.
  • $@: Represents all the arguments passed to the script or function as separate words.
  • $*: Represents all the arguments passed to the script or function as a single word.
  • $?: Represents the exit status of the last command executed.
  • $$: Represents the process ID of the current shell.
  • $!: Represents the process number of the last background command.

Using the Special Variables

Let's see how to use these special variables with some examples:

#!/bin/bash
echo "Script Name: $0"

function func {
  for var in $*; do
    let i=i+1
    echo "The \$${i} argument is: ${var}"
  done
  echo "Total count of arguments: $#"
}

func We are argument

In the above example, we define a function called func that takes multiple arguments. We use the special variables $* and $# to iterate over the arguments and determine their count.

Create a file called ~/project/special.sh.

cd ~/project
chmod +x special.sh
./special.sh
Script Name: ./special.sh
The $1 argument is: We
The $2 argument is: are
The $3 argument is: argument
Total count of arguments: 3

Understanding the Difference Between $@ and $*

There is a difference in behavior when these special variables, $@ and $*, are enclosed in double quotes. Let's see an example:

#!/bin/bash
function func {
  echo "--- \"\$*\""
  for ARG in "$*"; do
    echo $ARG
  done

  echo "--- \"\$@\""
  for ARG in "$@"; do
    echo $ARG
  done
}

func We are argument

In the above example, we define a function func that takes multiple arguments. We iterate over the $* and $@ variables. When enclosed in double quotes, $* treats all arguments as a single word, whereas $@ treats them as separate words.

Revise the file ~/project/special.sh and run it again.

Script Name: ./special.sh
--- "$*"
We are argument
--- "$@"
We
are
argument

Summary

In this lab, you learned about the special variables in shell scripting. These variables provide valuable information about the script execution environment. You can use these variables to retrieve the script name, process ID, command-line arguments, and more. Understanding and utilizing these special variables will help you write more robust and flexible shell scripts.

Other Linux Tutorials you may like