Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The - (hyphen or minus) character in Bash is a heavily overloaded lexical token. Its behavior is strictly determined by its syntactic context, functioning variously as an arithmetic operator, a parameter expansion modifier, a special parameter, a file descriptor closure token, a pattern matching range indicator, a conditional test prefix, or a directory navigation shortcut.

Arithmetic Operators

Within an arithmetic evaluation context ($(( )), (( )), let, or variables typed with declare -i), - functions as a binary subtraction operator, a unary negation operator, and forms the unary pre-decrement and post-decrement operators (--).

# Binary subtraction
(( result = operand1 - operand2 ))


# Unary negation
(( result = -operand ))


# Pre-decrement and post-decrement
(( --operand ))
(( operand-- ))

File Descriptor Closure

In redirection operations, appending - to the file descriptor duplication operators (>& or <&) instructs the shell to close the specified file descriptor rather than duplicating it.

# Close standard output (fd 1)
>&-


# Close standard input (fd 0)
<&-


# Close a specific file descriptor (e.g., fd 3)
3>&-

Special Parameter (Shell Options)

The hyphen acts as a core special parameter ($-). When expanded, it yields a string containing the current set of single-letter shell option flags enabled in the current Bash environment (e.g., himBH).

# Expands to current shell options
echo $-

Parameter Expansion (Default Value Substitution)

In brace parameter expansion, - acts as a fallback operator. It instructs the shell to evaluate to a specified default string if the target parameter is unset. When combined with a colon (:-), it triggers the fallback if the parameter is either unset or null (empty).

# Substitutes 'default_value' only if 'var' is strictly unset
${var-default_value}


# Substitutes 'default_value' if 'var' is unset OR null
${var:-default_value}

Pattern Matching Range Operator

Within bracket expressions [ ] used in pathname expansion (globbing) and regular expression matching ([[ =~ ]]), - defines a contiguous character range based on the current locale’s collating sequence.

# Globbing character range
ls *[a-z]*


# Regular expression character range
[[ $string =~ [0-9] ]]

Conditional Test Prefix

Within the test builtin, POSIX brackets [ ], and Bash extended test keywords [[ ]], - is the required prefix for unary file/string operators and binary algebraic comparison operators.

# Unary operator prefix (e.g., -z for zero-length string, -f for regular file)
[[ -z $variable ]]


# Binary operator prefix (e.g., -eq for arithmetic equality)
[[ $val1 -eq $val2 ]]

Here-Document Tab Stripping

When appended to the here-document redirection operator (<<), forming <<-, the hyphen modifies the parsing of the here-document. It instructs the shell to strip all leading tab characters (but not spaces) from the subsequent lines of the document body and the terminating delimiter.
command <<-DELIMITER
	text_with_leading_tabs
DELIMITER

Previous Working Directory Alias

When passed as the sole argument to the cd builtin, - is parsed as a reserved alias for the $OLDPWD environment variable. It instructs the shell to change the current working directory to the previous working directory and prints the new path to standard output.
cd -

Option Prefix and End-of-Options Marker

The - character is the standard prefix for short (single-character) options passed to Bash builtins and external commands. Furthermore, a standalone double hyphen (--) is parsed as the end-of-options indicator, forcing all subsequent arguments to be treated as positional parameters (operands), even if they begin with a -.

# Short option prefix
command -x -y


# End-of-options marker
command -- -filename_starting_with_hyphen
Master Bash with Deep Grasping Methodology!Learn More