What is command line argument in linux

Command Line Arguments in Linux

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.
[email protected] $* 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.
Читайте также:  Linux group domain users

Execute the bash script using this command:

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 “$ ”. 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:

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 “[email protected]” and “$*”. The below script takes two command line arguments from the user and lists the arguments:

Execute the bash script using this command:

The user passed two arguments, NewName and USA, that are listed by the script using [email protected].

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:

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:

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:

Читайте также:  Mac level astra linux

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 "[email protected]" do echo "User - $i: $fruit"; i=$((i + 1)); done

Run the bash script using this command:

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.

Источник

How to Use Command Line Arguments in a Bash Script

In general, there are many ways to pass input to programming or scripting languages and sometimes there is a need to pass input from the command line instead of input from the code. Like any other programming or scripting language bash scripting also support the command-line argument.

In this article, we will try to explore the syntax for command-line arguments and then we will see some examples for command-line arguments and discuss the special variables.

Command-line Argument Syntax

We already know how to run a bash script in Linux. We can use either bash or sh command and then the bash file name.

Now, to pass the command-line argument we can pass arguments separated by space.

Here is an actual example

Next, we are going to try some command-line argument bash scripting.

Approach 1: Pass three integers through the command line and get the sum

Example

#!/bin/bash echo "First argument is--> $1" echo "Second argument is--> $2" echo "Third argument is--> $3" sum=$((1+$2+$3)) echo "Sum = $sum"

Then run the bash script like

Output

First argument is--> 1 Second argument is--> 2 Third argument is--> 12 Sum = 15

Explanation

From the above scripting, we have got below points

  • First argument is stored in $1
  • Second argument is stored in $2
  • Third argument is stored in $3

Approach 2: List of special variables used in bash scripting

  • $1…$n − This is a positional argument. We already know this from the previous example.
  • $0 − This indicates the scripting file name. Example: sum.sh
  • $# − This holds the total number of arguments passed.
  • $@ − This holds the values of each argument.
  • $$ − This holds the PID of the current shell.
  • $* − Using this we can get all the arguments in a single parameter.
  • $? − This holds the exit status id of the recent command.
  • $! − This holds the PID of the recent command.
Читайте также:  Linux usb ethernet rndis gadget драйвер

Let us use all these in a script and see the output.

Example

#!/bin/bash echo "1. 1st argument is --> $1" echo -e "1. 2nd argument is --> $2" echo "2. File name is --> $0" echo "3. Total number of arguments passed --> $#" echo "4." i=1; for arg in "$@" do echo "argument-$i is : $arg"; i=$((i + 1)); done echo "5. PID of current shell --> $$" echo "6." i=1; for arg in "$*" do echo "argument-$i is: $arg"; i=$((i + 1)); done echo "Executing ls command" ls echo "7. Exit status of last command --> $?" echo "Executing firefox command" firefox & echo "8. PID of last command --> $!" killall firefox

Output

$ bash special-var.sh 1 "2 4" 5 1. 1st argument is --> 1 1. 2nd argument is --> 2 4 2. File name is --> special-var.sh 3. Total number of arguments passed --> 3 4. argument-1 is : 1 argument-2 is : 2 4 argument-3 is : 5 5. PID of current shell --> 3061 6. argument-1 is: 1 2 4 5 Executing ls command special-var.sh sum.sh 7. Exit status of last command --> 0 Executing firefox command 8. PID of last command --> 3063

Approach 3: Use of Flags

There is a way to pass a command-line argument along with a flag. Flag is a single letter starting with a hyphen before the argument.

Let us see the code and then it will be easy to understand the concept.

Example

#!/bin/bash while getopts c:s:d: flag do case "$" in d) district=$;; s) state=$;; c) country=$;; esac done echo "District: $district"; echo "State: $state"; echo "Country: $country";

Output

$ bash flag.sh -c INDIA -d BANGALORE -s KARNATAKA District: BANGALORE State: KARNATAKA Country: INDIA

From the above code, we can see the advantages of using the flag in the command line. Now we know that we do not have to pass the command line arguments in a sequence. Instead, we can use flag.

Conclusion

In this article, we have learned how to use command-line arguments in bash script in Linux. In addition, various special variables help to parse the command line arguments. So, using the command line argument we can do better and more efficient bash scripting.

Источник

Оцените статью
Adblock
detector