User Input & Validation with read
In shell scripting, managing data dynamically is essential for building interactive and automated workflows. This chapter covers the fundamental concepts of variables, how the shell distinguishes variables from plain text using the dereference operator ($), how to modify file permissions to make scripts executable, and how variables dynamically update during execution.
1. Why Do We Need Variables?
In any programming or scripting language, hardcoding values limits the reusability of your code. If you hardcode a name like "Jetha" throughout your script, changing it later requires manually searching and replacing every instance.
Variables solve this problem by acting as named storage locations. Instead of referencing a static value, you reference a container whose value can change dynamically based on user input, system state, or script logic.
2. Core Mental Model: The "Vary-able"
Real-Life Analogy
The Nickname Analogy: The creator shares a personal anecdote: at home, family members affectionately call him "Daddu" (a common nickname in Marathi/Hindi for an elder brother or grandfather figure). While his official name remains constant, his nickname or how people address him can vary depending on the context.
Similarly, a Variable is literally something that is "Vary-able"—its label remains the same, but the value it holds can change at any time.
3. Syntax & Variable Dereferencing (The Power of $)
When you define a variable in Bash, you assign a value to a key without spaces:
NAME="Jetha"
The Problem of Identification
If you want to print this variable using the echo command, you cannot simply write:
echo "Name is NAME"
Output:
Name is NAME
Without a special marker, the shell cannot distinguish between the literal string "NAME" and the variable NAME. It treats everything inside the quotes as plain text.
The Solution: The Dollar Sign ($)
To tell the shell to extract the value stored inside the variable (a process called dereferencing or variable substitution), you must prefix the variable name with a dollar sign ($):
echo "Name is $NAME"
Output:
Name is Jetha
4. Step-by-Step Script Creation & Execution Flow
To run a shell script in Linux, you must write the code, grant execution permissions, and run it via the terminal.
The Execution Workflow
5. Understanding File Permissions (chmod 755)
By default, newly created text files in Linux do not have execution permissions. If you try to run them, the system will deny access. To grant execution rights, we use the chmod command.
The creator uses the command:
chmod 755 jethalal_ki_duniya.sh
Permission Breakdown
The number 755 represents octal permissions for three distinct user classes:
Note: In many Linux terminal configurations, running
chmod 755turns the file name green (referred to in the lecture as getting the "hari jhandi" or green flag), indicating that the file is now an executable script.
6. Complete Code Evolution
Let us look at how the script behaves when we modify the variable value from "Jetha" to "Babita Ji".
Version 1: Initial Script with "Jetha"
Create a file named jethalal_ki_duniya.sh:
#!/bin/bash
# Defining the variable
NAME="Jetha"
# Printing the variable using dereferencing
echo "Name is $NAME"
Execution & Output:
$ chmod 755 jethalal_ki_duniya.sh
$./jethalal_ki_duniya.sh
Name is Jetha
Version 2: Dynamic Update to "Babita Ji"
If we open the script and update the value of the NAME variable:
#!/bin/bash
# Updating the variable value
NAME="Babita Ji"
# Printing the updated variable with a comma
echo "Name is $NAME,"
Execution & Output:
$./jethalal_ki_duniya.sh
Name is Babita Ji,
Because the variable is "vary-able", the script dynamically adapts to the new value without requiring any changes to the underlying echo logic.
Summary and Key Takeaways
- Variables are Dynamic Containers: A variable's value can change ("vary") throughout the lifecycle of a script.
- No Spaces in Assignment: When assigning a variable in Bash, do not leave spaces around the
=sign (e.g.,NAME="Jetha"). - The
$Prefix is Mandatory for Reading: Use the$symbol to dereference a variable and access its stored value. Without$, Bash treats it as a literal string. - File Permissions Matter: Use
chmod 755 <script_name>to grant read, write, and execute permissions to the owner, and read/execute permissions to everyone else. - Visual Cues: Successfully changing permissions to executable often changes the file color to green in the terminal directory listing.
- Execution Syntax: Run local scripts using the
./prefix (e.g.,./script.sh) to specify the current directory path.