Shell script variables are named memory locations used to store data that can be referenced and manipulated throughout a script. They allow scripts to be dynamic and flexible by holding values such as text, numbers, or the output of commands. Variables in shell scripts help reduce redundancy, enhance readability, and make automation tasks more efficient. They are defined without data types and can be easily modified during script execution, making them essential tools for controlling flow, configuring behavior, and managing data in Unix-based scripting environments.
Shell script variables
User-Defined Variables
These are variables created by the user in a shell script or directly in the terminal.
Syntax:
variable_name=value
No spaces around
=
.
Example:
name="Nish"
echo "Hello, $name"
Important Points:
- Use
$variable_name
to access the value. - To avoid issues, wrap variables in double quotes (
"$var"
).
Environment Variables
These are predefined variables provided by the system or the user for the shell environment.
Used by the OS and shell to configure behavior.
Examples:
HOME
– your home directoryPATH
– list of directories to search for commandsUSER
– your usernameSHELL
– path to the current shell
Usage:
echo $HOME
echo $PATH
Exporting a Variable:
To make a user-defined variable available to child processes:
export myvar="hello"
Special Variables
These are read-only shell variables that give info about the script or shell environment.
Variable | Description |
---|---|
$0 | Name of the script |
$1, $2...$n | Positional parameters (arguments passed to the script) |
$# | Number of arguments passed |
$@ | All arguments (as separate words) |
$* | All arguments (as a single word) |
$$ | PID of the current shell |
$? | Exit status of the last command |
$! | PID of the last background process |
Example:
#!/bin/bash
echo "Script name: $0"
echo "1st argument: $1"
echo "Number of args: $#"
Summary:
Type | Who defines it? | Scope | Example |
---|---|---|---|
User-defined | You | Current shell | myname="Nish" |
Environment | System/User | Child shells too | export PATH |
Special | Shell itself | Read-only | $0 , $# , $? |
Shell Script: demo.sh
#!/bin/bash
# User-defined variable
username="Nish"
# Environment variable (export it so child processes can access it)
export project="EmbeddedSystem"
# Special variables
echo "Script name : $0"
echo "1st argument : $1"
echo "2nd argument : $2"
echo "Total args passed : $#"
echo "All args (\$@) : $@"
echo "Current user : $USER"
echo "Home directory : $HOME"
echo "Custom username : $username"
echo "Project name : $project"
echo "Script PID : $$"
# Example of command and checking exit status
ls /tmp
echo "Exit status of 'ls' : $?"
How to Run:
chmod +x demo.sh
./demo.sh arg1 arg2
Output Example:
Script name : ./demo.sh
1st argument : arg1
2nd argument : arg2
Total args passed : 2
All args ($@) : arg1 arg2
Current user : nish
Home directory : /home/nish
Custom username : Nish
Project name : EmbeddedSystem
Script PID : 12345
Exit status of 'ls' : 0
Frequently Asked Questions (FAQ)
What is user input in Bash?
User input in Bash refers to the data entered by a user while a script is running. This allows the script to behave dynamically based on the values provided at runtime.
How do I take input from a user in a Bash script?
You can use the read
command. Example:
read -p "Enter your name: " name
echo "Hello, $name!"
Can I hide user input in Bash (like for passwords)?
Yes! Use the -s
option with read
to hide the input:
read -sp "Enter your password: " password
How do I set a default value if no input is provided?
You can use parameter expansion:
read -p "Enter your city [New York]: " city
city=${city:-New York}
echo "You chose $city"
Can I read multiple inputs at once?
Absolutely! Just list multiple variable names after read
:
read -p "Enter your first and last name: " first last
echo "Welcome, $first $last!"
Is user input available after the script ends?
No. Bash variables—including those storing input—exist only while the script is running, unless you explicitly save them (e.g., in a file).
How do I validate or restrict input?
You can use conditional statements or loops to check input:
while true; do
read -p "Enter a number: " num
[[ "$num" =~ ^[0-9]+$ ]] && break
echo "Please enter a valid number."
done
You can also Visit other tutorials of Embedded Prep
- What is eMMC (Embedded MultiMediaCard) memory ?
- Top 30+ I2C Interview Questions
- Bit Manipulation Interview Questions
- Structure and Union in c
- Little Endian vs. Big Endian: A Complete Guide
- Merge sort algorithm
Special thanks to @mr-raj for contributing to this article on Embedded Prep
Leave a Reply