Fundamentals 5 min read

Mastering Shell Special Parameters: $*, $@, $#, $$, and $! Explained with Real Examples

This article explains the behavior of key Bash special parameters—$*, $@, $#, $$, and $!—through three practical scripts that demonstrate argument expansion, counting, arithmetic operations, and process ID retrieval, highlighting the differences and typical use cases.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Shell Special Parameters: $*, $@, $#, $$, and $! Explained with Real Examples

The article introduces several special parameters used in Bash scripting: $*, $@, $#, $$, and $!, and explains how each one works.

Example 1 – Expanding positional parameters with $* and $@

#!/bin/bash

export IFS='-'
cnt=1
# Printing the data available in $*
echo "Values of \"$*\":"
for arg in "$*"
do
  echo "Arg #$cnt= $arg"
  let "cnt+=1"
done

cnt=1
# Printing the data available in $@
echo "Values of \"$@\":"
for arg in "$@"
do
  echo "Arg #$cnt= $arg"
  let "cnt+=1"
done

Running the script with ./expan.sh "Hello world" 2 3 4 shows that $* produces a single string "Hello world-2-3-4" (joined by the IFS delimiter), while $@ treats each argument separately, yielding four distinct lines.

Example 2 – Using $# to count arguments and perform arithmetic

#!/bin/bash
if [ $# -lt 2 ]
then
  echo "Usage: $0 arg1 arg2"
  exit
fi

echo -e "$1=$1"
echo -e "$2=$2"

let add=$1+$2
let sub=$1-$2
let mul=$1*$2
let div=$1/$2

echo -e "Addition=$add
Subtraction=$sub
Multiplication=$mul
Division=$div
"

The script first checks that at least two positional parameters are supplied; if not, it prints a usage message. It then displays the two arguments and calculates their sum, difference, product, and integer division using the let builtin.

Example 3 – Accessing process IDs with $$ and $!

#!/bin/bash
echo -e "Process ID=$$"

sleep 1000 &
echo -e "Background Process ID=$!"

When executed, $$ prints the PID of the running script, and $! prints the PID of the most recent background command (the sleep process). The article also shows a sample ps output confirming the IDs.

Key takeaways

$*

expands all positional parameters into a single word, separated by the current IFS value. $@ expands each positional parameter as a separate word, preserving quoting. $# provides the count of positional parameters passed to the script. $$ returns the PID of the current shell process. $! returns the PID of the most recently executed background command.

Understanding these parameters enables more precise control over argument handling, script flow, and process management in Bash scripts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ShellScriptingBashprocess IDspecial-parameters
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.