Xin Tao Xin Tao - 9 months ago 53
Bash Question

What's the usages of hyphen("-") in linux shell?

We all know that when we use hyphen after the command

, we will change to the previous working directory; and when we use hyphen after the input redirection
, the bash will ignore the leading tabs (but not spaces) in the here script. The output from the cat command will not contain any of the leading tab characters. Is there any other usages of the hyphen character? I'm a fresh man in linux, please show more.

Answer Source


I skimmed the man page, this should be pretty complete. I learned some things.

Many built-in commands use a hyphen for options, like declare.

-- declares the end of options to a shell builtin.

$- "expands to the current option flags as specified upon invocation".

-N unary minus.

N--, --N pre and post increment operators.

N - M subtraction.

N -= M subtract and assign

file1 -X file2 and -X file are file test operators

~- will expand to the value of $OLDPWD.

-X string are various string operators.

-o optname to check if an option is enabled.

-X varname various variable operators.

~-N, where N is a number, will expand to the value in the directory stack.

arg1 -op arg2 various binary comparison operators.

${parameter:-word} uses word as the default if the parameter is unset.

[a-d] in a pattern match indicates a character set range.

>&- and <&- indicates to duplicate and close a file descriptor.

<<- indicates a here document to strip leading tab characters.

%- previous job.

M-x and \M- are used to display a meta character.

C-x and \C- are used to display a control character.

Readline variables can have - in their name.

x-y are a range of words when selecting from an event.

expr1 -X expr2 boolean logic and/or test operators