How can you access the first and second command-line arguments in a shell script?

098

In a shell script, you can access command-line arguments using the special variables $1, $2, etc. Here’s a simple example:

#!/bin/bash

# Accessing the first and second command-line arguments
first_argument=$1
second_argument=$2

echo "First Argument: $first_argument"
echo "Second Argument: $second_argument"

To run this script, save it to a file (e.g., script.sh), make it executable with chmod +x script.sh, and then execute it with two arguments:

./script.sh arg1 arg2

This will output:

First Argument: arg1
Second Argument: arg2

0 Comments

no data
Be the first to share your comment!