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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/FunctionsandScopeGroup(["`Functions and Scope`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/FunctionsandScopeGroup -.-> shell/func_def("`Function Definition`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/echo -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} linux/cd -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} linux/chmod -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/shebang -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/comments -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/quoting -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/variables_decl -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/variables_usage -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/str_manipulation -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/param_expansion -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/for_loops -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/func_def -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} shell/globbing_expansion -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} linux/wildcard -.-> lab-153903{{"`Special Variables in Shell Scripting`"}} end

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