Shell Scripting - Variables

Опубликовано: 03 Май 2026
на канале: AA Software Technologies
7
0

1. Environment Variables
These are predefined by the system and available to all processes. You can also create your own.
Variable Description Example Output ( depends on system)
$USER Current logged-in username shanthakumar
$HOME Path to the home directory /home/shanthakumar
$PATH System path for executable files /usr/bin:/bin:/usr/sbin:...
$SHELL Current shell in use /bin/bash
$HOSTNAME Hostname of the system devops-machine
$PWD Current working directory /home/user/scripts

2. Positional Parameters
These are used to access command-line arguments passed to a script.
Variable Description Example
$0 Script name ./myscript.sh
$1 First argument arg1
$2 Second argument arg2
$# Total number of arguments 2
$* All arguments as a single string "arg1 arg2"
$@ All arguments as individual items "arg1" "arg2"

3. Special Variables
These provide metadata or special status during execution.
Variable Description Example Use
$$ Current script’s process ID (PID) echo $$
$? Exit status of last command (0 = success) ls xyz; echo $?
$! PID of the last background command sleep 30 & echo $!