Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Positional Parameters in Unix OS and Shell Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Positional Parameters in Unix OS and Shell Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Positional Parameters in Unix OS and Shell Programming

In Unix and Unix-like operating systems, positional parameters are variables that hold the
arguments or input passed to a script or command. These parameters are numbered, allowing
you to access and manipulate them easily in shell scripts.

Here’s a breakdown:

1. Accessing Positional Parameters:

• $0: Refers to the name of the script or command itself.

• $1, $2, ..., $N: Refers to the first, second, ..., Nth argument passed to the script.

For example, in a script invoked like this:

./myscript.sh arg1 arg2 arg3

$0 is ./myscript.sh

$1 is arg1

$2 is arg2

$3 is arg3

Special Parameters:

• $#: Represents the number of arguments passed to the script.

• $*: Expands to all arguments as a single word.

• $@: Expands to all arguments but treats each argument as a separate word.

• "$@": Useful in loops; it treats each argument as a separate quoted string, unlike $*.

• $$: The process ID (PID) of the current script.

• $?: The exit status of the last command executed.

• $!: The PID of the last background process.

echo "Script name: $0"

echo "First argument: $1"

echo "Second argument: $2"

echo "Number of arguments: $#"

echo "All arguments (using \$*): $*"

echo "All arguments (using \$@): $@"

Differences between $* and $@:


• $*: Treats all arguments as a single string. If quoted, it returns a single word with all
arguments joined by the first character of IFS (Internal Field Separator, typically a
space).

• $@: Treats each argument separately. When quoted, each argument is individually
quoted and separated.

For instance, given arg1 arg2 arg3 as inputs:

• $* expands to "arg1 arg2 arg3"

• $@ expands to "arg1" "arg2" "arg3"

Examples

# Using "$@"

for arg in "$@"; do

echo "Argument: $arg"

done

# Using "$*"

for arg in "$*"; do

echo "Argument: $arg"

done

In this example, "$@" iterates over each argument separately, while "$*" treats all arguments as
one string.
Unix shell script for addition of two numbers using user input

# Prompt the user to input the first number

echo "Enter the first number:"

read num1

# Prompt the user to input the second number

echo "Enter the second number:"

read num2

# Perform the addition

sum=$((num1 + num2))

# Output the result

echo "The sum of $num1 and $num2 is: $sum"

When executed, it will ask for two numbers and then display the sum.

Unix shell script for addition of two numbers using positional parameters

# Check if two arguments are provided

if [ $# -ne 2 ]; then

echo "Usage: $0 number1 number2"

exit 1

fi

# Perform the addition

sum=$(($1 + $2))

# Output the result

echo "The sum of $num1 and $num2 is: $sum"


Using Positional Parameters in Conditional Statements

if [ $# -ne 2 ]; then

echo "You must provide exactly 2 arguments."

exit 1

fi

echo "First argument: $1"

echo "Second argument: $2"

Shifting Positional Parameters

echo "First argument before shift: $1"

shift

echo "First argument after shift: $1"

Combining Positional Parameters with Arithmetic Operations

if [ $# -ne 2 ]; then

echo "Usage: $0 number1 number2"

exit 1

fi

sum=$(( $1 + $2 ))

echo "The sum of $1 and $2 is: $sum"

You might also like