Fundamentals 4 min read

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

This article explains the purpose and behavior of key shell special parameters—$*, $@, $#, $$, and $!—through three practical scripts that demonstrate expanding positional arguments, counting arguments, and accessing process IDs, complete with code snippets and expected output.

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

In this guide we introduce several special parameters used in Bash scripts: $*, $@, $#, $$ and $!. Each parameter’s behavior is demonstrated with a concrete example.

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

The script expan.sh sets IFS='-' and iterates over the arguments using both "$*" and "$@". The output shows that $* treats all arguments as a single string separated by the internal field separator, while $@ yields each argument individually.

#!/bin/bash
export IFS='-'
cnt=1
echo "Values of \"$*\":"
for arg in "$*"
do
  echo "Arg #$cnt= $arg"
  let "cnt+=1"
done

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

Running ./expan.sh "Hello world" 2 3 4 produces:

Values of "$*":
Arg #1= Hello world-2-3-4
Values of "$@":
Arg #1= Hello world
Arg #2= 2
Arg #3= 3
Arg #4= 4

Example 2 – Counting arguments with $#

The script count.sh checks whether the number of supplied arguments is less than two. If so, it prints a usage message; otherwise it displays the first two arguments and performs basic arithmetic on them.

#!/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"

Sample execution:

$ ./count.sh 2314 15241
$1=2314
$2=15241
Addition=17555
Subtraction=-12927
Multiplication=35267674
Division=0

Example 3 – Process‑related parameters $$ and $!

The script proc.sh prints its own process ID using $$, starts a background sleep command, and then prints the background job’s PID via $!.

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

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

Typical output:

Process ID=14625
Background Process ID=14626

These examples illustrate how $* concatenates all positional parameters into a single string, $@ treats each parameter separately, $# reports the count of arguments, $$ gives the script’s own PID, and $! provides the PID of the most recent background command.

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.

ShellScriptingBashcommand-linespecial-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.