How to Print Second Command Line Argument in Shell?
The shell stores the command line arguments in a positional way. The first argument is stored in $1, the second in $2, and other arguments follow this pattern. Below we have listed the methods that we will discuss in this article for accessing command line Arguments:
- Method 1: Using positional parameters
- Method 2: Using ‘shift‘ to access other arguments
- Method 3: Using Awk
- Method 4: Using a for loop
- Method 5: Using Sed or Grep
- Method 6: Using an Array
So, let’s discuss all the possible methods to print the second command line argument in the shell.
Method 1: Using positional parameters
Step 1: Open a text editor and paste the following script into it:
#!/bin/bash
# Shell script to print the second command line argument
echo "The second command line argument is: $2"
In this script, we are accessing the 2nd command-line argument using “$2” and printing it using the echo command.
Step 2: Save this script to a file (e.g. script.sh), and then run it with the desired arguments, like so:
sh script.sh <First argument> <Second argument>
Method 2: Using ‘shift’
The shift command shifts the positional parameters to the left, discarding the value of $1 and moving $2 to $1, $3 to $2, and so on.
#!/bin/bash
# Shift once to reach the second argument
shift
# Print the second argument
echo "The second command line argument is: $1"
This script uses the shift keyword to move to the next command-line argument, skipping the first one, and then it prints out the second argument.
Output:
Method 3: Using Awk
Awk is a powerful text-processing tool that can also be used to handle command-line arguments. Here’s a simple example:
#!/bin/bash
# Shell script to print the second command line argument using awk
echo "$@" | awk '{print "The second command line argument is: " $2}'
In this script, “$@” is used to represent all command line arguments. Awk is then used to print the second argument.
Output:
Method 4: Using a for loop
You can also use a loop to iterate through command line arguments. This method is particularly useful when you need to perform a specific action on each argument individually.
#!/bin/bash
# Print the second argument
# Loop through each command-line argument
for arg in "$@"; do
# Check if this is the second argument
if [ "$arg" = "$2" ]; then
echo "The second command line argument is: $arg"
break
fi
done
This script utilizes a for loop to iterate through all the command line arguments “$@”, and after printing the second argument it breaks the loop.
Output:
Method 5: Using sed and grep
If you need to filter or manipulate the command line arguments based on a specific pattern, tools like sed or grep can be useful.
Here’s an example script to find 2nd cli argument using sed and grep command:
#!/bin/bash
# Use grep to find the second command-line argument
second_argument=$(echo "$@" | grep -oP '\S+' | sed -n '2p')
# Check if the second argument was found
echo "The second command line argument is: $second_argument"
This script uses grep with the -oP options to match non-whitespace characters (\S+). The sed command is then used to extract the second match (2p).
To learn more about grep and sed check out these articles, grep command in Unix/Linux and sed Command in Linux.
Output:
Method 6: Using an Array:
You can also store command line arguments in an array for easier manipulation. Here’s an example:
#!/bin/bash
# Convert command-line arguments into an array
args=("$@")
# Access and print the second command-line argument
echo "The second command line argument is: ${args[1]}"
This script creates an array named args containing all command line arguments and then prints the second command line argument stored at index 1.
Output:
Contact Us