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.
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 : $?"Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
