Fundamentals 3 min read

Shell Positional and Predefined Variables

The article introduces common shell positional and predefined variables, explains their purposes, and demonstrates their usage through two example scripts that illustrate arithmetic operations and parameter handling with echo statements and special variable references.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Shell Positional and Predefined Variables

This article explains common shell positional and predefined variables such as $#, $*, $$, $!, $@, $-, and $?, describing their meanings and typical uses.

It then provides two example scripts. The first script ( 01.sh) demonstrates adding two arguments using expr and printing the result.

Example script content:

#!/bin/bash
SUM=$(expr $1 + $2)
echo "$1 + $2 = $SUM"

The second script ( param.sh) shows how to display the script’s path, name, and each positional parameter using echo and the special variables described earlier.

Example script content:

#!/bin/sh
# $0: file full path name
echo "path of script : $0"
# $0 basename
echo "name of script : $(basename $0)"
# $1 … $5
echo "parameter 1 : $1"
echo "parameter 2 : $2"
echo "parameter 3 : $3"
echo "parameter 4 : $4"
echo "parameter 5 : $5"
# $# and $*
echo "The number of arguments passed : $#"
echo "Show all arguments : $*"
# $$ and $?
echo "Process ID : $$"
echo "errors : $?"
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.

Bashpositional-parameters
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

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.