The command-line arguments in Linux are the parameters that the user provides via the terminal to the script, which uses them in the shell script. It points to the memory position of the command and its parameters that take the user input. It passes it to the shell script for execution to perform the specific task according to user input.
This article will explain the uses of command-line arguments in Linux with examples of the following topics:
- How to Use Command Line Arguments in Linux?
- Show Script Name
- Pass Command Line Arguments From Terminal to Bash Script
- Get Arguments Passed to Script
- Find the Total Arguments Passed to Script
- Get the Exit Code
- Get Process ID
- Use Command Line Arguments in For Loop
How to Use Command Line Arguments in Linux?
Bash scripts receive command line arguments from the terminal. Several command line arguments are built-in, which take the user input to perform a specific function. The pre-defined command line arguments in Linux are:
$0 | It shows the script’s name. |
$1 | It shows the first command line argument. |
$2 | It takes the second argument from the terminal. |
$# | A script’s total number of arguments is displayed with this option. |
$@ $* | Both options provide the list of arguments given to the bash script. |
$? | It displays the exit code for the execution of the script. |
$$ | It shows the process ID (PID) for the script. |
Note: This article will use the bash script named “arguments.sh”.
The command line arguments are utilized in Linux in the following ways.
Example 1: Show Script Name
The command line arguments are very useful while automating the tasks using the bash scripts. The command line argument predefined to get the script name is “$0”. To below shell script is used to print the text with echo command and script name:
#!/bin/bash
echo "Welcome! To itslinuxfoss"
echo "The script name is: $0"
Let’s explain the bash script line-by-line:
- #!/bin/bash: Runs the script using bash.
- echo: Display the text.
- $0: Shows the script name.
Execute the bash script using this command:
$ bash arguments.sh
The output shows that the script name is displayed using the command line arguments “$0”, and this method is very useful while using the conditional loops depending on the script name.
Example 2: Pass Command Line Arguments From Terminal to Bash Script
The command line arguments are given to the script via the terminal to make it interactive. The script can take the arguments using the “$<number>”. The below script takes 3 arguments ($1, $2, $3) from the user via the terminal and displays the output:
#!/bin/bash
echo "Welcome! To itslinuxfoss"
echo "Your name is: $1"
echo "Your age is: $2"
echo "Your profession is: $3"
Execute the bash script using this command:
$ bash arguments.sh
Three arguments, TestName, 22, and Content-Writer, are passed by the user in the script.
Example 3: Get Arguments Passed to Script
The list of arguments passed by the user can be listed using the “$@” and “$*”. The below script takes two command line arguments from the user and lists the arguments:
#!/bin/bash
echo "Enter your name: $1"
echo "Enter your location: $2"
echo "Total arguments passed are: $@"
Execute the bash script using this command:
$ bash arguments.sh
The user passed two arguments, NewName and USA, that are listed by the script using $@.
Example 4: Find Total Arguments Passed to Script
When you need to find how many arguments are passed by the user, utilize the “$#” argument as shown below:
#!/bin/bash
echo "Your favorite fruit: $1"
echo "Your favorite color: $2"
echo "Number of arguments passed are: $#"
Execute the bash script using this command:
$ bash arguments.sh
The user passed two arguments, Mango and Black, which the script displays the number of arguments as 2.
Example 5: Get the Exit Code
The exit code can be found using the bash script that shows the status of the execution. The exit code depends on the state of execution like exit code is 0 for successful execution, while the 1-255 exit codes are for errors. For example, the below code displays the “Hello” text using the echo command and shows the script status with “$?”:
#!/bin/bash
echo "Hello!"
echo "The script exit code is: $?"
Execute the bash script using this command:
$ bash arguments.sh
The script shows the exit code is 0, which means the execution is successful.
Example 6: Get Process ID
The bash script can be used to find the process ID PID with the command line argument. The “$$” shows the process ID of the script as shown below:
#!/bin/bash
echo "Hello!"
echo "The process ID of the script is: $$"
Execute the bash script using this command:
$ bash arguments.sh
The output shows that the PID of the script is “3197” using the bash script.
Example 7: Using Command Line Arguments in For Loop
The command line arguments are commonly used in loops to automate the process. For example, the below script will display the number of arguments and their value given by the user with the for loop:
#!/bin/bash
i=1;
for fruit in "$@"
do
echo "User - $i: $fruit";
i=$((i + 1));
done
Run the bash script using this command:
$ bash arguments.sh
The user gives 3 arguments, Mango, Apple, and Banana which are displayed one by one with the for loop.
Conclusion
The command line arguments allow the users to pass the input from the terminal to the bash script, which makes the bash script interactive. The command line arguments can be used to automate the processes using the predefined arguments that are explained with examples in this guide.