What is positional parameters ($1, $2, $@) in Linux?

In Linux shell scripting, positional parameters are special variables that hold the values of the arguments passed to a script or a function. They allow you to refer to these arguments easily within your scripts.

Positional Parameters Overview

Here are the main types of positional parameters:

  • $1, $2, $3, ...: Represent the first, second, third, etc., arguments passed to the script.
  • $@: Represents all the arguments passed to the script as separate words.
  • $#: Represents the number of arguments passed to the script.

Example

Below is a simple example of a shell script that demonstrates the use of positional parameters:

#!/bin/bash echo "First argument: $1" echo "Second argument: $2" echo "All arguments: $@" echo "Total number of arguments: $#"

Positional parameters Linux scripting shell scripting $1 $2 $@