Yves
Yves

Reputation: 12431

What is the hyphen used for in shell script argument parsing?

I don't know why we use a hyphen for parameters of Shell. As I know, $1, $2 ... are the first, second parameter.

So my question is: When do/don't we need a hyphen for a parameter?

For me, when I write my own shell scripts, I always pass parameters without a hyphen. For example, myShell.sh param1 param2 param3.

However, for other shell scripts, we use hyphens. For example ls -l myDir. If I'm right, there are two parameters for ls: -l and myDir. Why do we use a hyphen for l but not for myDir?

Upvotes: 1

Views: 1349

Answers (1)

Fazlin
Fazlin

Reputation: 2337

Hyphen (-) is used to differentiate the options with other arguments.

I am using your example ls -l myDir to explain. How will you differentiate whether l is option to output long lists or you want to list a file named l?

Upvotes: 2

Related Questions