Skip to content

Uses of the Dollar Sign ($) in Shell Scripting

UsageDescription
$variableVariable Expansion: Retrieves the value of the variable variable.
$(command) or $(command)Command Substitution: Executes command and substitutes its output.
$0Special Variable: Represents the name of the script itself.
$1, $2, $3, ...Special Variables: Represent the positional parameters passed to the script or function. $1 refers to the first parameter, $2 to the second, and so on.
$@Special Variable: Represents all the positional parameters as separate words.
$#Special Variable: Represents the number of positional parameters.
${!variable}Variable Indirect Expansion: Uses the value of variable as the name of another variable to retrieve its value indirectly.
$(())Arithmetic Expansion: Evaluates the arithmetic expression enclosed within the double parentheses and substitutes the result.
$?Special Variable: Holds the exit status of the last executed command.
$!Special Variable: Represents the process ID (PID) of the most recently executed background command.
$$Special Variable: Represents the process ID (PID) of the shell.
"$variable" or "${variable}"Variable Expansion with Double Quotes: Expands the variable variable while preserving spaces and allowing for the expansion of special characters.
'$variable' or '${variable}'Variable Expansion with Single Quotes: Treats the variable variable as a literal string without expansion.
$((expression))Arithmetic Expansion: Evaluates the arithmetic expression enclosed within double parentheses and substitutes the result (alternative syntax to $(())).
${variable:-default} or ${variable-default}Parameter Expansion with Default Value: Expands the variable variable, but if it is unset or null, substitutes the default value.
${variable:=default}Parameter Expansion with Default Value Assignment: Expands the variable variable, but if it is unset or null, assigns it the default value.
${variable:+alternate} or ${variable+alternate}Parameter Expansion with Alternate Value: Expands the variable variable, but if it is set and non-null, substitutes the alternate value.
${variable:?error_message}Parameter Expansion with Error Message: Expands the variable variable, but if it is unset or null, prints the error_message and exits the script with a non-zero status.
${variable:offset:length}Substring Extraction: Extracts a substring from the value of variable starting at offset and with a length of length.
${variable#pattern} or ${variable##pattern}Pattern Removal: Removes the shortest (#) or longest (##) match of pattern from the beginning of the value of variable.
${variable%pattern} or ${variable%%pattern}Pattern Removal: Removes the shortest (%) or longest (%%) match of pattern from the end of the value of variable.
${variable/pattern/replacement}Pattern Replacement: Replaces the first occurrence of pattern with replacement in the value of variable.
${variable//pattern/replacement}Pattern Replacement: Replaces all occurrences of pattern with replacement in the value of variable.
${#variable}String Length: Returns the length of the value of variable.