How to use Flags In Linux

Arguments can be passed along with the flags. The arguments can be identified with a single letter having – before that. A single letter can be meaningful and here let us take -1, -2, and -3.

We need to use getopts function to read the flags in the input, and OPTARG refers to the corresponding values:

Program2 using flags

The above program can be executed as

sh usingFlags.sh -1 ‘w3wiki’ -2 ‘JavaProgramming’ -3 100

Output

Output

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.

Similar Reads

Positional Parameters

Command-line arguments are passed in the positional way i.e. in the same way how they are given in the program execution.  Let us see with an example....

Using Flags

Arguments can be passed along with the flags. The arguments can be identified with a single letter having – before that. A single letter can be meaningful and here let us take -1, -2, and -3....

Using Loops with $@ – Loop Constructs

Though positional parameters help to send as command-line arguments, if there are more arguments, we cannot count and use them. Instead by using $@, we can achieve that. It is nothing but the array of all the parameters passed. Iterating over a for loop, we can get that....

Conclusion

We can pass command-line arguments at our convenience in a positional way or with flags. Moreover useful concepts like $@ (array of all the parameters), $# (by calculating input size)  are available and with that, we can iterate in the  “for” or “while” loops, we can access the arguments....