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 # symbol in Bash is a heavily overloaded lexical and syntactic operator. Depending on its context, it functions as a comment initiator, a parameter expansion operator (for length evaluation, prefix stripping, and front-anchoring), an array element counter, a base-radix indicator in arithmetic evaluation, a history expansion designator, a format flag in builtins, and a prompt string escape sequence.

Lexical Parsing: Comments

When # appears as the first character of an unquoted and unescaped word (meaning it is located at the very beginning of a line or is immediately preceded by an unquoted metacharacter such as a space, tab, newline, |, &, ;, (, ), <, or >), the shell treats it and all subsequent characters on that line as a comment, discarding them during the tokenization phase. In interactive shells, this behavior is strictly governed by the interactive_comments shell option, which is enabled by default but can be disabled via shopt -u interactive_comments.

# This entire line is ignored by the parser
echo "String" # This is ignored from the hash onward
echo "String"# This is NOT a comment because # is not preceded by a metacharacter
echo "String" ; # This IS a comment because ; is a metacharacter separating words

Parameter Expansion: Length Evaluation

Within parameter expansion syntax, prepending a variable name with # evaluates to the length in characters of the expanded value of that parameter.
length=${#parameter}

Parameter Expansion: Prefix Substring Removal

When # is placed after a parameter name (or strictly after the subscript for arrays) and followed by a pattern, it acts as a prefix-stripping operator. The shell matches the pattern against the beginning of the parameter’s value.
  • Single # (Shortest Match): Deletes the shortest matching prefix.
  • Double ## (Longest Match): Deletes the longest matching prefix.
${parameter#pattern}      # Removes shortest matching prefix
${parameter##pattern}     # Removes longest matching prefix
${array[@]#pattern}       # Applies shortest prefix removal to each array element

Parameter Expansion: Pattern Substitution Front-Anchor

In pattern substitution (${parameter/pattern/replacement}), prefixing the pattern with # acts as a front-anchor. This forces the pattern match to occur strictly at the beginning of the expanded string.
${parameter/#pattern/replacement}

Array Length Evaluation

When applied to an array using the @ or * subscripts, the # operator evaluates to the total number of populated elements within that indexed or associative array, rather than the character length of a single string.
count=${#array_name[@]}

Special Parameter: Positional Count

As a standalone special parameter, # evaluates to the decimal integer representing the total number of positional parameters passed to the current script or function context.
$#

Arithmetic Evaluation: Base/Radix Notation

Within arithmetic expansion contexts (such as $(( )), (( )), or the let builtin), the # operator separates a base (radix) from a numeric value. The base must be a literal decimal integer token between 2 and 64. Because the base must be a literal token, unexpanded variable names cannot be used directly with the # operator; variables must be explicitly expanded.
result=$(( 16#FF ))
result=$(( $base#$number ))

History Expansion: Command Line Designator

In history expansion, the !# designator refers to the entire command line typed so far before the current point.
echo "arg1" "arg2" !#

Builtin Formatting: printf Alternate Flag

Within the printf builtin, # acts as an alternate format flag modifying the output of numeric conversions:
  • Octal/Hexadecimal: For %o, %x, or %X conversions, it forces the output to be prefixed with 0, 0x, or 0X, respectively.
  • Floating-Point: For %f, %e, %E, %g, and %G conversions, it forces the output to always contain a decimal point. For %g and %G, it additionally prevents the truncation of trailing zeros.
printf "%#x\n" 255
printf "%#g\n" 123

Prompt String Escape Sequence

In Bash prompt strings (such as PS1 or PS2), \# is a built-in escape sequence that evaluates to the command number of the current command.
PS1="[\#] $ "
Master Bash with Deep Grasping Methodology!Learn More