Day 40 – Bash Scripting (Part 3)

Topics Covered:
✅ Quotes in Bash
✅ Command Line Substitution
✅ Exporting Variables
✅ Reading User Input

📌 1. Quotes in Bash

Single Quotes ' '

  • Treat everything literally
echo 'Hello $USER'  # Output: Hello $USER

Double Quotes " "

  • Interpret variables & command substitution
echo "Hello $USER"  # Output: Hello shaharyar

No Quotes

  • Word splitting and pathname expansion (globbing) can occur
echo Hello $USER   # Still works but not always safe

📌 2. Command Substitution

Used to assign output of a command to a variable or inline.

Syntax: command or $(command)

current_date=$(date)
echo "Today is $current_date"

$(...) is recommended (easier to nest)


📌 3. Exporting Variables

Variables set with export are made available to child processes (e.g., another script, bash, or program).

export MY_NAME="Shaharyar"
bash -c 'echo $MY_NAME'  # Output: Shaharyar

Without export: variables are local to the current shell.


📌 4. Reading User Input

Use read to take input from the user.

echo "Enter your name:"
read name
echo "Welcome, $name!"

Multiple inputs:

read first last
echo "Hello $first $last"

Prompt on the same line:

read -p "Enter your age: " age

🧭 What’s Next?

Decision making in bash

0
Subscribe to my newsletter

Read articles from Shaharyar Shakir directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shaharyar Shakir
Shaharyar Shakir