
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bash Special Variables in Linux
Introduction
Bash (Bourne Again SHell) is default shell for most Linux systems. It is a command language interpreter that executes commands from standard input, file or command-line arguments. Bash shell provides a set of special variables that contain various system-related and user-related information. These special variables are used to manipulate and access data efficiently. In this article, we will explore bash special variables in Linux with examples.
What are Bash Special Variables?
Bash special variables are predefined variables that are used to store system and user-related information. They start with "$" symbol and are used in bash scripts to perform various operations. Some of bash special variables are ?
$0 ? It is used to store name of script or shell that is currently executing.
$# ? It stores number of command-line arguments that were passed to script.
$* ? It stores all command-line arguments passed to script as a single string.
$@ ? It stores all command-line arguments passed to script as separate strings.
$? ? It stores exit status of last executed command.
$! ? It stores process ID of last background process that was executed.
$IFS ? It stores Internal Field Separator, which is used to split a string into fields.
Accessing Bash Special Variables
Bash special variables are accessed using "$" symbol followed by variable name. For example, to access name of script currently executing, we can use following command ?
echo $0
This will output name of script.
Similarly, to access number of command-line arguments passed to script, we can use following command ?
echo $#
This will output number of command-line arguments.
$0 Variable
The $0 variable stores name of script or shell that is currently executing. It is used to get name of script to display it in output or to check if script is running. Consider following example ?
Example
#!/bin/bash echo "The name of script is: $0"
Output
The name of script is: script.sh
$# Variable
The $# variable stores number of command-line arguments passed to script. It is used to check number of arguments before processing them. Consider following example ?
Example
#!/bin/bash echo "The number of arguments is: $#"
Output
If we run script with three command-line arguments, we will get following output ?
The number of arguments is: 0
$* and $@ Variables
The $* and $@ variables store all command-line arguments passed to script as a single string and separate strings, respectively. $* variable separates arguments with first character of IFS variable, while $@ variable separates arguments with spaces. Consider following example ?
Example
#!/bin/bash echo "The arguments using \$* are: $*" echo "The arguments using \$@ are: $@"
Output
If we run script with three command-line arguments, we will get following output ?
The arguments using $* are: The arguments using $@ are:
$? Variable
The $? variable stores exit status of last executed command. It is used to check whether last command executed successfully or not. exit status of a command is an integer value between 0 and 255, where 0 means success and any non-zero value indicates an error. Consider following example ?
Example
#!/bin/bash ls echo "The exit status of last command is: $?"
Output
main.sh The exit status of last command is: 0
In this example, ls command lists files in current directory, and echo command displays exit status of last command. Since ls command executes successfully, exit status is 0.
$$
The $$ variable stores process ID (PID) of currently running script or shell. It is used to identify process that is running script. Consider following example ?
Example
#!/bin/bash echo "The process ID of current script is: $$"
Output
The process ID of current script is: 1929919
In this example, echo command displays PID of current script.
$! Variable
The $! variable stores process ID (PID) of last background process that was executed. It is used to check status of a background process or to terminate it if needed. Consider following example ?
Example
#!/bin/bash sleep 10 & echo "The process ID of last background process is: $!"
Output
The process ID of last background process is: 12346
In this example, sleep command is executed in background, and echo command displays PID of background process.
$IFS Variable
The $IFS variable stores Internal Field Separator, which is used to split a string into fields. It is used to specify character used to separate fields in a string. By default, IFS variable is set to whitespace, tab, and newline characters. Consider following example ?
Example
#!/bin/bash IFS=: echo "Field 1:Field 2:Field 3" | while read f1 f2 f3 do echo "Field 1: $f1" echo "Field 2: $f2" echo "Field 3: $f3" done
Output
Field 1: Field 1 Field 2: Field 2 Field 3: Field 3
In this example, IFS variable is set to ":" (colon), and echo command outputs a string containing three fields separated by colons. while loop reads string and splits it into fields using IFS variable, and echo commands display individual fields.
$LINENO Variable
The $LINENO variable is used to display current line number in a bash script. It is particularly useful in debugging scripts, where you want to know which line of code caused a particular error. Consider following example ?
Example
#!/bin/bash echo "Line number: $LINENO" echo "Line number: $LINENO"
Output
Line number: 3 Line number: 4
In this example, echo command displays value of $LINENO variable, which is automatically set to line number of script at time of execution.
$BASH_SOURCE and $FUNCNAME Variables
The $BASH_SOURCE and $FUNCNAME variables are used in functions to display name of script or function that is currently being executed. $BASH_SOURCE variable displays name of script or function that was executed, while $FUNCNAME variable displays name of current function. Consider following example ?
Example
#!/bin/bash function func1 { echo "Function name: ${FUNCNAME[0]}" echo "Script name: ${BASH_SOURCE[0]}" } function func2 { func1 } func2
Output
Function name: func1 Script name: /path/to/script.sh
In this example, we define two functions, func1 and func2, and call func2. Inside func2, we call func1, which displays values of $FUNCNAME and $BASH_SOURCE. Since func1 is current function, its name is displayed as value of $FUNCNAME, and name of script is displayed as value of $BASH_SOURCE.
$SECONDS Variable
The $SECONDS variable is used to display number of seconds that have elapsed since script started running. It is particularly useful in measuring execution time of a script or a particular section of a script. Consider following example ?
Example
#!/bin/bash echo "Script started at $(date)" sleep 5 echo "Elapsed time: $SECONDS seconds"
Output
Script started at Wed Mar 15 14:26:58 IST 2023 Elapsed time: 5 seconds
In this example, we use $SECONDS variable to display number of seconds that have elapsed since script started running. sleep command is used to pause execution of script for 5 seconds, and elapsed time is displayed after sleep command.
Conclusion
Bash special variables are an essential part of bash scripting in Linux. They provide a simple and efficient way to access and manipulate system and user-related information. In this article, we explored some of commonly used bash special variables, such as $0, $#, $*, $@, $?, $$, $!, and $IFS, along with examples of their usage. By using these variables in our scripts, we can enhance functionality and usability of our bash scripts.