What does “ — “ (double-dash) mean in the bash shell? And the difference between arguments and options.
Hi Guys, Today we are going to learn about the usage of the double dash “- -” in the shell. But before that let's understand the problem that I faced exactly and how that was resolved by using the “- -”
Problem:
Please take a look at the below screenshot clearly, most of the time Linux commands are failing because of “-” at the beginning of the filename.
To understand more about this problem and the solution for this, let's take a step back and learn about the commands and their options, arguments, and then parameters concepts. Please check the below screenshot for a better understanding.
By observing the above screenshot, everything (which is separated by white space by default) is an argument including the command i.e., $0.
Every argument is classified into 3 types ( intentionally ignoring the Linux sub-commands for making it simple.)
- $0: This is the first argument. this is nothing but command/program like rm, cp, and some_program.sh
- Options: Every argument which starts with — (hyphen) considers options to $0. These arguments control the functionality of the command.
For example:
*. grep pattern file: search for the pattern in the file
*. grep -v pattern file: after adding -v options it completely changes the behavior, now it looks for the patterns which won’t match. - Parameters: Remaining all the arguments are called parameters.
Now if you check the first screenshot, we will get the actual problem. Hope you spot the issue. Because -file3 starts with -(Hyphen), command (rm, cat..) taking it as an option, not as a parameter.
Finally, we have understood the problem (at least, I am hoping 😉). So we can think of the solutions. A better way to fix this is by explicitly telling the shell to consider -file3 as a parameter, not an option using the double dash.
Man page definition for double dash : A — signals the end of options and disables further option processing. Any arguments afterthe — are treated as filenames and arguments. An argument of — is equivalent to — .
After double dash ( — — ) everything is considered as a parameter type. That is why all the commands are succeded this time.
Note: All Linux commands don’t follow this syntax but those are very few.
I Hope, You liked this article. More articles related to DevOps tools, Linux and AWS on the way so please follow me for getting the notifications. Bye for now 😀.