๐Ÿงฉ 2. Bash Basics & Syntax
Estimated reading: 3 minutes 44 views

๐Ÿš Bash Special Variables โ€“ $?, $#, $@, $0, $1 Explained with Examples


๐Ÿงฒ Introduction to Bash Special Variables โ€“ Understand Script Arguments & Exit Status

Bash includes a powerful set of special variables that provide insight into script arguments, command success, process IDs, and more. These built-in variables begin with a dollar sign ($) and serve different roles when writing or debugging scripts.

Whether you’re automating tasks or handling input parameters, knowing how to use Bash’s special variables like $?, $#, $@, $0, and $1 is essential.


๐ŸŽฏ In this article, youโ€™ll learn:

  • What Bash special variables are and how to use them
  • The purpose of $0, $1, $@, $#, and $?
  • Real examples of how they behave in scripts
  • How to debug scripts using these variables

๐Ÿ“Œ List of Common Bash Special Variables

VariableDescription
$0Name of the script itself
$1 โ€“ $9Positional parameters (first to ninth argument)
$@All arguments as separate words
$*All arguments as a single word
$#Number of arguments passed
$?Exit status of the last command
$$Process ID (PID) of the current shell
$!PID of the last background command

๐Ÿงช Example โ€“ Script Using Special Variables

๐Ÿ“ args-demo.sh

#!/bin/bash

echo "Script name: $0"
echo "1st argument: $1"
echo "2nd argument: $2"
echo "All arguments (\$@): $@"
echo "Number of arguments: $#"

ls /nonexistentdir
echo "Exit status of last command: $?"

โ–ถ๏ธ Run the Script:

chmod +x args-demo.sh
./args-demo.sh one two

โœ… Output:

Script name: ./args-demo.sh
1st argument: one
2nd argument: two
All arguments ($@): one two
Number of arguments: 2
ls: cannot access '/nonexistentdir': No such file or directory
Exit status of last command: 2

๐Ÿ” Detailed Explanation of Key Variables

๐Ÿ”ธ $0 โ€“ Script Name

Returns the name of the script as it was called.

echo "Running $0"
# Output: Running ./myscript.sh

๐Ÿ”ธ $1, $2, …, $9 โ€“ Positional Parameters

Access individual arguments passed to the script.

echo "First input: $1"

๐Ÿ”ธ $@ โ€“ All Arguments (Preserves Quoting)

Treats each parameter as a separate word.

for arg in "$@"; do
  echo "$arg"
done

๐Ÿ”ธ $* โ€“ All Arguments (Single String)

Treats all arguments as a single string.

echo "$*"

๐Ÿ”ธ $# โ€“ Argument Count

Returns the number of arguments passed.

echo "There are $# arguments"

๐Ÿ”ธ $? โ€“ Exit Status of Last Command

Returns the exit code of the last command.

  • 0 means success
  • Any other number indicates failure
mkdir myfolder
echo "Status: $?"  # Output: 0 (if success)

๐Ÿ”ธ $$ โ€“ Current Shell PID

Useful for creating unique filenames in temp scripts.

echo "Current PID: $$"

๐Ÿ”ธ $! โ€“ Last Background Process ID

When a command runs in the background (&), $! stores its process ID.

sleep 10 &
echo "Sleep PID: $!"

๐Ÿ“Œ Summary: Bash Special Variables

Bash special variables unlock dynamic scripting and make shell scripts more robust, interactive, and debuggable. Use them to handle arguments, track errors, and control background processes effectively.

๐Ÿ” Key Takeaways:

  • $0, $1โ€“$9 handle script names and arguments
  • $@ and $* differ in how they preserve input
  • $# tells how many arguments were passed
  • $? helps in tracking command success/failure

โš™๏ธ Real-world Uses:

  • Writing parameterized automation scripts
  • Checking command execution results in cron jobs
  • Capturing process IDs for cleanup or logs

โ“ FAQ โ€“ Bash Special Variables


โ“ What is the difference between $@ and $* in Bash?
โœ… $@ treats each argument as a separate quoted string, while $* merges them into a single string. Example:

for arg in "$@"; do echo "$arg"; done  # each argument separately
echo "$*"  # all arguments as one

โ“ How do I know how many arguments a script received?
โœ… Use $#:

echo "Argument count: $#"

โ“ What is $? used for in Bash?
โœ… $? gives the exit status of the last executed command. 0 means success, non-zero means failure.


โ“ What does $$ mean in Bash?
โœ… It holds the process ID (PID) of the currently running shell.


โ“ How do I use $! in a script?
โœ… Use it to capture the PID of a command run in the background:

command &
echo "PID: $!"

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐ŸŸข Bash: Special Variables ($?, $#, $@, $0, $1, etc.)

Or Copy Link

CONTENTS
Scroll to Top