LinuxCommandLibrary

$

Execute a command as a user

TLDR

Print a variable

$ echo $[VARIABLE]
copy

Run variable contents as a command
$ $[VARIABLE]
copy

Print the exit status of the previous command
$ echo $?
copy

Print a random number between 0 and 32767
$ echo $RANDOM
copy

Print one of the prompt strings
$ echo $[PS0|PS1|PS2|PS3|PS4]
copy

Expand with the output of command and run it. Same as enclosing command in backtics
$ $([command])
copy

List how many arguments the current context has
$ echo $#
copy

Print out a Bash array
$ echo $[{array_name[@]]}
copy

SYNOPSIS

The dollar sign ($) is not a command with a traditional synopsis. Its usage is context-dependent and follows various patterns:

$VARIABLE_NAME
$SPECIAL_PARAMETER
$(COMMAND)
${PARAMETER_EXPANSION_SYNTAX}

As a prompt indicator: user@host:~$
In regular expressions: pattern$

PARAMETERS

$VARIABLE
    Expands to the value of the shell variable or environment variable named VARIABLE (e.g., $HOME, $PATH).

$0, $1, $2, ...
    Positional parameters where $0 is the name of the script/shell function, and $1, $2, etc., are the command-line arguments passed to it.

$?
    Expands to the exit status of the most recently executed foreground pipeline (0 for success, non-zero for failure).

$$
    Expands to the process ID (PID) of the shell itself.

$!
    Expands to the process ID (PID) of the most recently executed background command.

$#
    Expands to the number of positional parameters passed to the shell or script.

$*
    Expands to all positional parameters as a single word (when unquoted), or as a single argument containing all positional parameters separated by the first character of IFS (when quoted as "$*").

$@
    Expands to all positional parameters. When quoted as "$@", each parameter expands as a separate word, preserving arguments containing whitespace or special characters. This is generally preferred over "$*".

$(command)
    Command substitution; the expression is replaced by the standard output of command. This is the modern and preferred syntax over backticks (`command`).

${parameter}
    General form for parameter expansion. The braces are used to delimit the parameter name from surrounding text (e.g., ${VAR}suffix) or to perform complex expansions like substring manipulation, default value assignment (e.g., ${VAR:-default}), and pattern removal.

$ (in RegEx)
    In regular expressions (e.g., with grep, sed, awk), the $ character matches the end of a line. For example, pattern$ matches lines ending with 'pattern'.

$N (in awk)
    In awk, $N refers to the Nth field of the current input record. For example, $1 is the first field, $2 is the second field, and $0 refers to the entire current record.

DESCRIPTION

The dollar sign ($) is a fundamental and highly versatile special character in the Linux shell environment, serving multiple distinct purposes. Crucially, it is not a command itself, but rather an operator or indicator whose meaning is heavily dependent on its context.

Its most common use is for variable expansion, where $VARIABLE_NAME retrieves and substitutes the value of a defined shell or environment variable (e.g., $HOME, $PATH). It also denotes various special shell parameters, such as $? (exit status of the last command), $$ (current shell's process ID), and $# (number of positional parameters passed to a script).

Beyond simple variable access, $ facilitates advanced parameter expansion using brace syntax (e.g., ${VAR:-default}), allowing for default values, substring manipulation, and more. It's also integral to command substitution (e.g., $(ls -l)), where the output of a command replaces the expression. In the context of regular expressions (used by commands like grep, sed), $ acts as an anchor, matching the end of a line. Lastly, it frequently appears as the default shell prompt indicator for non-root users, signifying readiness for command input.

CAVEATS

The behavior of $ is highly context-dependent, which can lead to confusion. Proper quoting (single quotes '...' vs. double quotes "...") is crucial; single quotes suppress all expansions, while double quotes allow variable and command substitution but prevent globbing and word splitting.

The older backtick (`command`) syntax for command substitution is deprecated in favor of $(command), as it's less prone to nesting issues and easier to read.

In regular expressions, $ needs to be escaped (e.g., \$) if you intend to match the literal dollar sign character, as it otherwise serves as an end-of-line anchor.

<I>SHELL PROMPT INDICATOR</I>

The $ character is commonly seen as the default prompt symbol for non-root users in many Linux and Unix-like systems (e.g., user@hostname:~$). It indicates that the shell is ready to accept commands. The root user typically has a different prompt symbol, usually #.

<I>DIFFERENCE BETWEEN <B>$VAR</B> AND <B>${VAR}</B></I>

While $VAR works for simple variable expansion, ${VAR} is often preferred. The braces are necessary when the variable name is immediately followed by characters that could be interpreted as part of the variable name itself. For example, to append 'suffix' to a variable VAR, you must use ${VAR}suffix, as $VARsuffix would look for a variable named 'VARsuffix'.

HISTORY

The usage of $ for variable expansion and special parameters dates back to the very early Unix shells, particularly the Bourne shell (sh) in the late 1970s. Its fundamental role in shell scripting has been carried forward and expanded upon in subsequent shells like ksh (Korn shell), bash (GNU Bourne-Again Shell), and zsh (Z Shell). The parameter expansion syntax (e.g., ${VAR:-default}) and $(command) for command substitution were introduced to provide more robust and less ambiguous ways to handle shell programming constructs, becoming standard in POSIX-compliant shells.

SEE ALSO

bash(1), sh(1), grep(1), sed(1), awk(1), regex(7)

Copied to clipboard