DEV Community

Parth Kamal
Parth Kamal

Posted on

Understanding the `$` Symbol in Bash Scripting

In Bash, the $ symbol appears everywhere — and for good reason.
Whether you’re working with variables, command substitution, arithmetic, or script arguments, $ is the gateway that lets you access underlying values.
Once you understand how it behaves, the shell becomes significantly easier to navigate.


What $ Actually Does

At the core, $ instructs Bash to:

“Expand this into its value.”

This applies consistently across variables, commands, expressions, and special parameters.
A single rule powering multiple forms of expansion.


Key Uses of $ in Bash

1. Working with Variables

name="Parth"
echo "$name"
Enter fullscreen mode Exit fullscreen mode

2. Quoting to Avoid Word Splitting

msg="Hello World"
echo $msg      # splits into separate words
echo "$msg"    # preserves the original string
Enter fullscreen mode Exit fullscreen mode

3. Using ${var} When Appending Text

file="log"
echo "${file}.txt"
Enter fullscreen mode Exit fullscreen mode

4. Capturing Command Output

today=$(date)
count=$(ls -1 | wc -l)

echo "$today"
echo "$count"
Enter fullscreen mode Exit fullscreen mode

5. Arithmetic Expansion

echo $((3 + 5))
n=4
echo $((n * 2))
Enter fullscreen mode Exit fullscreen mode

6. Special Shell Parameters Every Script Uses

Exit code of the previous command

echo $?
Enter fullscreen mode Exit fullscreen mode

PID of the current shell

echo $$
Enter fullscreen mode Exit fullscreen mode

Script name

echo $0
Enter fullscreen mode Exit fullscreen mode

Positional parameters (arguments passed to the script)

Running:

./script.sh apple banana cherry
Enter fullscreen mode Exit fullscreen mode

Inside the script:

echo $1    # apple
echo $2    # banana
echo $3    # cherry
echo $#    # total arguments (3)
echo "$@"  # all arguments as separate values
echo "$*"  # all arguments as one combined string
Enter fullscreen mode Exit fullscreen mode

7. Indirect and Meta Expansions

sleep 5 &
echo $!    # PID of the most recent background job
Enter fullscreen mode Exit fullscreen mode
user_1="Alice"
user_2="Bob"
echo ${!user_*}   # prints variable names starting with 'user_'
Enter fullscreen mode Exit fullscreen mode

8. A Common Beginner Error

result = $((1 + 2))    # incorrect — spaces make this invalid
result=$((1 + 2))      # valid form
Enter fullscreen mode Exit fullscreen mode

The Underlying Principle

All these examples rely on one idea:

$ transforms a reference into the evaluated value behind it.

  • $name → gets a variable’s value
  • $(command) → gets command output
  • $(( )) → gets an arithmetic result
  • $0 → gets the script’s name
  • $1, $2, … → get argument values
  • $? → gets the previous command’s exit code

A single symbol enabling a wide spectrum of expansions.


Conclusion

Understanding the role of $ brings clarity to Bash scripting.
Once this expansion model clicks, the shell feels far more structured and logical.

Top comments (0)