Variable Substitution and Command Substitution
Variable Substitution
- Definition: Accessing the value of a variable using the
$symbol is called variable substitution. - Syntax:
It is recommended to use
$variablename ${variablename}
${variablename}for clarity, especially in cases where additional text is appended to the variable value.
Example Script (test.sh):
#! /bin/bash
a=10
b=20
COURSE="linux"
ACTION="SLEEP"
echo "Values of a and b are: $a and $b"
echo "My Course is: ${COURSE}"
echo "My Favourite Action: $ACTIONING"
echo "My Favourite Action: ${ACTION}ING"Output:
Values of a and b are: 10 and 20
My Course is: linux
My Favourite Action:
My Favourite Action: SLEEPING
Explanation:
$ACTIONINGdoes not substitute because there is no variableACTIONING.${ACTION}INGappendsINGto the value ofACTION, resulting inSLEEPING.
Key Notes:
- Using single quotes (
') prevents variable substitution. - Using double quotes (
") allows variable substitution.
Example:
NAME="durga"
echo NAME # Output: NAME
echo $NAME # Output: durga
echo '$NAME' # Output: $NAME
echo "$NAME" # Output: durgaQuiz:
Q: Consider the variable declaration NAME="durga". Which of the following prints the value of NAME?
A) echo NAME
B) echo $NAME
C) echo '$NAME'
D) echo "$NAME"
Answer: B, D
Command Substitution
- Definition: Execute a command and substitute its result within a script or command.
- Syntax:
- Old style:
`command`(backticks, not single quotes). - New style:
$(command)(recommended for readability).
- Old style:
Examples:
-
Getting the current date:
echo "Today Date is: `date +%D`" echo "Today Date is: $(date +%D)"
Output:
Today Date is: 12/06/19 Today Date is: 12/06/19 -
Counting files in the current directory:
echo "Number of files in Current Working Directory: `ls | wc -l`" echo "Number of files in Current Working Directory: $(ls | wc -l)"
Output:
Number of files in Current Working Directory: 5 Number of files in Current Working Directory: 5 -
Counting lines in a file (
test.sh):echo "The No of Lines in test.sh: `cat test.sh | wc -l`" echo "The No of Lines in test.sh: $(wc -l test.sh)"
Output:
The No of Lines in test.sh: 9 The No of Lines in test.sh: 9 test.sh -
Getting the current working directory:
echo "My Current Working Directory: `pwd`" echo "My Current Working Directory: $(pwd)"
Output:
My Current Working Directory: /home/durgasoft/scripts My Current Working Directory: /home/durgasoft/scripts
Comparison of Substitutions
| Type | Definition | Syntax | Example | Output |
|---|---|---|---|---|
| Variable Substitution | Replaces variable name with its value | $variablename or ${variablename} |
echo $NAME or echo ${NAME} |
Prints the value of NAME. |
| Command Substitution | Replaces command with the result of its execution | `command` or $(command) |
echo $(date) or echo `date` |
Prints the output of the command. |
Case Study: Automating a System Monitoring Report Using Variable and Command Substitution
Scenario:
A company wants to generate a system monitoring report automatically and display the following details using a shell script:
- Current Date and Time.
- Logged-in User's Name.
- Current Working Directory.
- Total Number of Files in the Current Directory.
- Free Disk Space.
The script should utilize Variable Substitution and Command Substitution to display this information dynamically.
Script: system_report.sh
#!/bin/bash
# Using Command Substitution to retrieve dynamic information
CURRENT_DATE=$(date '+%Y-%m-%d %H:%M:%S')
LOGGED_USER=$USER
WORKING_DIRECTORY=$(pwd)
TOTAL_FILES=$(ls | wc -l)
DISK_SPACE=$(df -h / | grep '/' | awk '{print $4}')
# Displaying the report using Variable Substitution
echo "-----------------------------------"
echo " System Monitoring Report "
echo "-----------------------------------"
echo "Date and Time : $CURRENT_DATE"
echo "Logged-in User : ${LOGGED_USER}"
echo "Current Working Directory : ${WORKING_DIRECTORY}"
echo "Total Number of Files : ${TOTAL_FILES}"
echo "Available Disk Space : ${DISK_SPACE}"
echo "-----------------------------------"Explanation:
-
Variable Substitution:
$LOGGED_USER,${WORKING_DIRECTORY},${TOTAL_FILES}, and${DISK_SPACE}are used to substitute the values stored in variables.
-
Command Substitution:
$(date '+%Y-%m-%d %H:%M:%S'): Fetches the current date and time.$(pwd): Retrieves the current working directory.$(ls | wc -l): Counts the total number of files in the directory.$(df -h / | grep '/' | awk '{print $4}'): Fetches the free disk space of the root partition.
Key Learning Points:
- Dynamic Command Execution: Using command substitution ensures that the script fetches the latest system information each time it runs.
- Readable Outputs: Variable substitution provides clear and formatted results, making the script output user-friendly.
- Reusable Code: The use of variables like
CURRENT_DATEandDISK_SPACEmakes the script easy to maintain and modify.
Case Study: Backup Automation Script Using Variable and Command Substitution
Scenario:
An organization needs to back up important files daily to a specific directory with the current date in the backup folder name. The backup process should:
- Create a directory named
backup_<date>. - Copy all
.txtfiles from a source directory to the backup directory. - Generate a log file containing the list of backed-up files.
- Provide a summary at the end, including the total number of files backed up and the destination path.
Script: backup.sh
#!/bin/bash
# Variables and Command Substitution
SOURCE_DIR="/home/akshay/Desktop/operations/session54"
BACKUP_DIR="/home/akshay/Desktop/operations/session54"
CURRENT_DATE=$(date '+%Y-%m-%d')
BACKUP_FOLDER="${BACKUP_DIR}/backup_${CURRENT_DATE}"
LOG_FILE="${BACKUP_FOLDER}/backup_log.txt"
# Create the backup folder
mkdir -p "${BACKUP_FOLDER}"
# Start the log file
echo "Backup Log - Date: ${CURRENT_DATE}" > "${LOG_FILE}"
echo "-----------------------------------" >> "${LOG_FILE}"
# Copy .txt files to the backup folder and log their names
cp "${SOURCE_DIR}"/*.txt "${BACKUP_FOLDER}" 2>/dev/null && ls "${BACKUP_FOLDER}"/*.txt >> "${LOG_FILE}"
# Count the number of files backed up
TOTAL_FILES=$(ls "${BACKUP_FOLDER}"/*.txt 2>/dev/null | wc -l)
# Display summary
echo "-----------------------------------"
echo " Backup Summary "
echo "-----------------------------------"
echo "Date of Backup : ${CURRENT_DATE}"
echo "Source Directory : ${SOURCE_DIR}"
echo "Backup Directory : ${BACKUP_FOLDER}"
echo "Total Files Backed Up : ${TOTAL_FILES}"
echo "Log File : ${LOG_FILE}"
echo "-----------------------------------"
# Handle case of no files to backup
if [ "${TOTAL_FILES}" -eq 0 ]; then
echo "No '.txt' files found in the source directory. Backup skipped."
rm -rf "${BACKUP_FOLDER}"
else
echo "Backup completed successfully! Check the log file for details."
fiExplanation:
-
Variable Substitution:
${SOURCE_DIR},${BACKUP_DIR},${CURRENT_DATE},${BACKUP_FOLDER}, and${LOG_FILE}hold the source, destination, date, and log paths, respectively.${TOTAL_FILES}is used to count and display the number of files backed up.
-
Command Substitution:
$(date '+%Y-%m-%d'): Gets the current date for naming the backup folder.$(ls "${BACKUP_FOLDER}"/*.txt | wc -l): Counts the number of.txtfiles in the backup directory.
-
Conditional Logic:
- If no
.txtfiles are found, the backup folder is removed, and the user is notified.
- If no
Key Learning Points:
- Dynamic Folder Names: Using command substitution ensures each backup has a unique name with the current date.
- Logging and Debugging: The log file records all backed-up files, which helps with future auditing and debugging.
- Error Handling: The script gracefully handles cases where there are no files to back up.
- Automating Repetitive Tasks: By integrating command and variable substitution, repetitive tasks like backups can be automated efficiently.