How to Print Second Command line Argument in shell?
Last Updated :
26 Dec, 2023
Command line arguments are the parameters we pass to a command or script when we run it from the terminal. These arguments are passed as strings and can be accessed within your program to perform various operations.
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>
Printing 2nd cli argument using positional parametersMethod 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:
Printing 2nd cli argument using shiftMethod 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:
Printing the second command line argument using awkMethod 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:
Printing 2nd cli argument using a for loopMethod 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:
Printing 2nd cli argument using sed and grepMethod 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:
Printing 2nd cli argument by storing all arguments in an arrayConclusion
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".
Similar Reads
How to Parse Command Line Arguments in Node ?
Command-line arguments, in the context of a command-line interface (CLI), are text strings that provide extra information to a program when it is executed.In Nodejs, these arguments are accessible through an array known as argv (arguments values), where the shell passes all received command-line arg
3 min read
How to print command line arguments passed to the script in Node.js ?
Node.js is an open-source and cross-platform runtime environment built on Chrome's V8 engine that enables us to use JavaScript outside the browser. Node.js helps us to use build server-side applications using JavaScript. In Node.js if you want to print the command line arguments then we can access
2 min read
Bash Script - How to use Command Line Arguments
In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line
4 min read
Sending Command Line Arguments to NPM Script
In the sector of NodeJS development, NPM (Node Package Manager) scripts are a effective tool for automating numerous tasks like strolling exams, building the mission, or starting a improvement server. However, there are times while you need to bypass arguments to these scripts to customize their beh
3 min read
Command Line Argument in Scala
The arguments which are passed by the user or programmer to the main() method are termed as Command-Line Arguments. main() method is the entry point of execution of a program. main() method accepts an array of strings. runtime. But it never accepts parameters from any other method in the program. Sy
2 min read
How to Pass Command Line Arguments to GDB in a Linux Environment?
The GNU Debugger, commonly known as GDB, is a powerful tool used for debugging and analyzing programs in Linux and other Unix-like operating systems. While GDB is commonly used to debug programs, it can also be used to launch programs with specific command line arguments. This article will walk you
6 min read
How to Fix the Invalid Argument Error on Linux
If you have ever run a Linux command in your system terminal and seen the Invalid Argument error, there could be a few reasons for this. It usually means that you used an argument (a special word or setting) that the command doesn't recognize, or your user account doesn't have permission to access t
5 min read
Command Line Arguments in Objective-C
In Objective-C, command-line arguments are strings of text that are passed to a command-line program when it is launched. They can be used to specify options or parameters that control the behavior of the program or to provide input data that the program can process. To access command-line arguments
2 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
Command Line Arguments in Golang
Command-line arguments are a way to provide the parameters or arguments to the main function of a program. Similarly, In Go, we use this technique to pass the arguments at the run time of a program. In Golang, we have a package called as os package that contains an array called as "Args". Args is an
2 min read