How to Print Second Command line Argument in shell?
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:
Frequently Asked Questions (FAQs)
Q1. How can I match non-whitespace characters using “grep”?
In “grep,” you can use the expression \S or \S+ to match one or more non-whitespace characters.
Q2. Explain the purpose of $@ in shell scripting for command line arguments.
$@ represents all command line arguments in shell scripting, facilitating easy access to each argument for tasks like looping or manipulation.
Q3. How does the ‘shift’ command work with command line arguments, and why use it?
‘shift’ in shell scripting moves positional parameters left, discarding $1 and shifting others. It’s useful for iterating through or processing all arguments without explicitly referencing them.
Q4. When and why use Awk, Sed, or Grep with command line arguments in a script?
Awk, Sed, and Grep are valuable for complex text processing or pattern matching in command line arguments, aiding in filtering, or manipulation based on patterns.
Q5. Explain storing and manipulating command line arguments using arrays in shell scripting.
By assigning args=(“$@”), shell scripts can store all arguments in the ‘args’ array, simplifying iteration and individual manipulation of each command line argument.
Conclusion
Handling command line arguments in shell scripts is a crucial skill for any developer. Depending on your specific requirements and the tools you’re comfortable with, you can choose the method that best fits your needs.
To learn more about command line arguments check out this “Bash Script – How to use Command Line Arguments“.
Contact Us