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/minus) character in Bash is a context-dependent, overloaded token whose mechanical behavior and parsing rules change entirely based on the syntactic construct in which it is evaluated. Below are the distinct technical implementations of the - operator within Bash:

Parameter Expansion

Inside parameter expansion constructs (${}), - serves distinct mechanical purposes depending on the syntax: 1. Default Value Modifiers It acts as a conditional modifier to provide fallback values. It evaluates the state of a variable and substitutes a provided string if the variable does not meet specific criteria.
  • ${parameter-word}: Substitutes word only if parameter is strictly unset.
  • ${parameter:-word}: Substitutes word if parameter is unset OR null (empty).
2. Negative Offsets and Lengths in Substring Expansion In substring expansion (${parameter:offset:length}), a negative offset instructs the parser to evaluate the index from the end of the string rather than the beginning. To prevent the parser from misinterpreting the - as the default value modifier (:-), a negative offset must be separated by a space or enclosed in parentheses. Additionally, the length parameter can also be negative. A negative length changes the mechanical behavior to evaluate as an offset from the end of the string rather than a standard character count, effectively excluding that many characters from the end of the result.

# Default value syntax
result="${variable-fallback_string}"
result="${variable:-fallback_string}"


# Substring expansion with negative offset and negative length syntax
result="${variable: -5:-2}"
result="${variable:(-5):-2}"

I/O Redirection (Closing and Moving File Descriptors)

In I/O redirection, appending - to a redirection operator or target file descriptor alters the handling of the standard streams.
  • Closing Descriptors: Appending - directly to the duplication operator (>&- or <&-) instructs the shell to close the specified standard stream or file descriptor.
  • Moving Descriptors: Appending - to a target file descriptor digit (e.g., >&1- or <&0-) instructs the shell to move the file descriptor rather than just duplicating it. The target descriptor is duplicated to the specified file descriptor, and then the original target descriptor is closed.

# Syntax for closing file descriptors
exec 0<&-
exec 1>&-


# Syntax for moving file descriptors
exec 3<&0-
exec 4>&1-

Here-Document Tab Stripping

Appending - to the here-document redirection operator (<<-) is a parser feature that instructs the shell to strip all leading tab characters (but not spaces) from the here-document body and the terminating delimiter.

# Syntax for tab-stripped here-document
cat <<-EOF
	document_body
EOF

Tilde Expansion

The ~- construct is a distinct, built-in Bash expansion. During the tilde expansion phase of parsing, ~- resolves to the value of the $OLDPWD environment variable (the previous working directory), mirroring how ~+ resolves to $PWD.

# Syntax
echo ~-

History Expansion

In interactive shells, the !- construct is a history expansion designator. It instructs the parser to refer to a command a specific number of lines back in the history list.

# Syntax
!-2

Range Operator (Globbing and Regular Expressions)

Within bracket expressions [ ] used for pathname expansion (globbing) or regular expression matching (via the =~ operator), - defines a character class range. It instructs the parser to match any character that falls sequentially between the starting and ending characters based on the current locale’s collating sequence.

# Range operator syntax
ls [a-z]*
[[ "string" =~ [0-9] ]]

Special Parameter $-

The $- construct is a fundamental Bash special parameter. It expands to a string consisting of the current set of shell option flags specified during shell invocation or modified dynamically via the set builtin.

# Syntax
echo "$-"

Job Control

In job control contexts, when - is used in conjunction with the job specifier %, it refers to the previous job in the shell’s job table (the job that was active before the current background job).

# Syntax
fg %-

Arithmetic Subtraction and Unary Negation

Within arithmetic expansion contexts ($(( )), (( )), or the let builtin), - functions as a standard mathematical operator. It serves as both a binary operator for subtraction and a unary operator for negation.

# Binary subtraction
(( result = a - b ))


# Unary negation
(( result = -a ))


# Post-decrement / Pre-decrement
(( a-- ))
(( --a ))

Test Command Flags (Conditionals)

Within the test builtin, single-bracket [ ], and double-bracket [[ ]] evaluation contexts, - serves as a prefix defining specific unary and binary evaluation operators. While not all operators require a hyphen (e.g., =, !=, <, >, =~, and the logical NOT operator !), it is a mandatory prefix for:
  • Unary File/String Operators: Tests the state of a single operand (e.g., -e for file existence, -z for zero-length string).
  • Binary Arithmetic Operators: Compares two integer operands (e.g., -eq, -lt, -ge).

# Unary syntax
[[ -z "$variable" ]]
[[ -e "/etc/passwd" ]]


# Binary syntax
[[ "$a" -eq "$b" ]]

Previous Working Directory Alias

When passed as the sole argument to the cd (change directory) builtin, - is intercepted by the shell and expanded to the value of the $OLDPWD environment variable. It simultaneously triggers a print of the new $PWD to standard output.

# Syntax
cd -

Option Prefix and End-of-Options Delimiter

During command-line argument parsing, - dictates how arguments are interpreted by builtins and external binaries.
  • Short Options: A single - prefixes single-character POSIX options.
  • Long Options: A double -- prefixes GNU-style multi-character options.
  • End of Options: A standalone -- signals the end of options to the internal argument parsing logic of many builtins and external commands (often implemented via getopt or getopts). It instructs the command’s internal parser, rather than the shell’s syntax parser, to treat all subsequent arguments strictly as positional operands, even if they begin with a -.

# Short option syntax
ls -l -a


# End-of-options syntax
grep -- "-pattern_starting_with_hyphen" file.txt

Standard Stream Placeholder (stdin/stdout)

By POSIX convention, when - is passed as a file path argument to many standard utilities (like cat, tar, or grep), it instructs the command to read from standard input (file descriptor 0) or write to standard output (file descriptor 1) instead of interacting with the filesystem. While implemented by the utilities rather than the Bash parser itself, it is a fundamental mechanical component of Bash pipeline architecture.

# Syntax (reading from stdin)
echo "data" | cat - file.txt


# Syntax (writing to stdout)
wget -O - http://example.com > output.html
Master Bash with Deep Grasping Methodology!Learn More