There is a list of Special Parameters or Variables that are available when working with Bash Shell that can be used with scripts or command line arguments.
Bash Parameter | Description | Usage |
---|---|---|
$0 |
Name of the script or the shell | To Identify the script or shell that is currently running |
$1 , $2 , $3 , ... |
Positional parameters or arguments passed to the script | To access individual command-line arguments |
$* |
All positional parameters as a single string | To represent all command-line arguments as a single string |
$@ |
All positional parameters as separate strings | To represent all command-line arguments as separate strings |
$# |
Number of positional parameters | To count the total number of command-line arguments |
$? |
Exit status of the last executed command | To indicate the success or failure of the last command |
$$ |
Process ID (PID) of the current script or shell | To represent the unique identifier of the running process |
$! |
Process ID (PID) of the last background command | To store the PID of the most recent background process |
$- |
Current options set for the shell | To show the currently active options for the shell |
$IFS |
Internal Field Separator | To determine how Bash splits words into fields |
$PWD |
Current working directory | To represent the directory in which the script is running |
$OLDPWD |
Previous working directory | To hold the path of the previous working directory |
$HOME |
Home directory of the current user | To point to the user's home directory |
$USER |
Username of the current user | To store the name of the user executing the script |
$HOSTNAME |
Hostname of the system | To represent the name of the system |
$RANDOM |
A random number | To provide a random number each time it is accessed |
Let's see examples of each of them
#!/bin/bash
# Special Parameters Example Script
# $0 - Name of the script or the shell
echo "Script name: $0"
# $1, $2, $3, ... - Positional parameters or arguments passed to the script
echo "First argument: $1"
echo "Second argument: $2"
echo "Third argument: $3"
# $* - All positional parameters as a single string
echo "All arguments as a single string: $*"
# $@ - All positional parameters as separate strings
echo "All arguments as separate strings:"
for arg in "$@"; do
echo "$arg"
done
# $# - Number of positional parameters
echo "Total number of arguments: $#"
# $? - Exit status of the last executed command
echo "Exit status of last command: $?"
# $$ - Process ID (PID) of the current script or shell
echo "Process ID: $$"
# $! - Process ID (PID) of the last background command
echo "Process ID of last background command: $!"
# $- - Current options set for the shell
echo "Current options: $-"
# $IFS - Internal Field Separator
echo "Internal Field Separator: $IFS"
# $PWD - Current working directory
echo "Current working directory: $PWD"
# $OLDPWD - Previous working directory
echo "Previous working directory: $OLDPWD"
# $HOME - Home directory of the current user
echo "Home directory: $HOME"
# $USER - Username of the current user
echo "Username: $USER"
# $HOSTNAME - Hostname of the system
echo "Hostname: $HOSTNAME"
# $RANDOM - A random number
echo "Random number: $RANDOM"
Output:
./special_parameters_example.sh
Script name: ./special_parameters_example.sh
First argument: hello
Second argument: 1
Third argument: 2
All arguments as a single string: hello 1 2 3
All arguments as separate strings:
hello
1
2
3
Total number of arguments: 4
Exit status of last command: 0
Process ID: 4662
Process ID of last background command:
Current options: hB
Internal Field Separator:
Current working directory: /Users/c2ctech/Desktop
Previous working directory:
Home directory: /Users/c2ctech
Username: c2ctech
Hostname: C2C-Tech-Mac.local
Random number: 14105

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Linux,
- Rename a directory using Linux/Unix command
- ls command to list only directories
- How to Restart or Reload Nginx Server Service on Linux
- 3 ways to clear screen on Linux Terminal
- ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]
- Copy entire directory using Terminal Command [Linux, Mac, Bash]
- Fix: sudo: unable to open Read-only file system
- Create Hidden File or Directory using Shell Command
- Command to know the installed Debian version?
- The Date Command and its usage [Linux - Unix - macOS]
- Fix - bash: man: command not found
- How to tar.gz a directory or folder Command
- How to Display content of a file in Terminal Screen?
- How to change bash terminal prompt string and color
- Sort ls command by last modified date and time
- Execute .bin and .run file Ubuntu Linux
- zsh hello world example
- How to check uptime of Linux/Unix/macOS system/server using console command?
- [Fix] Linux - bash: useradd: command not found
- Command to check Last Login or Reboot History of Users and TTYs
- How to install and Configure sar sysstat tools in Ubuntu Linux
- How to use SCP Command to Copy Directory
- Linux Remove or Delete Files and Directories using Terminal Commands
- How to connect to SSH port other than default 22
- Install OpenSSL on Linux/Ubuntu
More Posts:
- Java: Collect Stream as ArrayList or LinkedList - Java
- Base 64 Encoding Decoding In Notepad++ - NotepadPlusPlus
- Easy Steps to Upgrade iPhone to the new iOS 16 - HowTos
- How to install Node using Brew on Mac - MacOS
- [Eclipse] Enable or Disable print margin line - Eclipse
- JDK Location in Android Studio - Android-Studio
- How to Stop Photos App from auto loading when device connected to the Mac - Mac-OS-X
- pwd Command - Print Working Directory - Linux