Variables and User Input
Learn to define and use variables in your scripts, and how to prompt users for input to make scripts interactive.
What are Variables?
In Bash scripting, variables are like named containers that hold data. Think of them as placeholders for information you want to use or change in your scripts.
They help make your scripts flexible, reusable, and easier to manage by storing values such as text, numbers, or even command outputs.
Declaring & Assigning Variables
To create a variable in Bash, you simply assign a value to a name. There are no special keywords like var or let needed.
- Use
VARIABLE_NAME=valuesyntax. - Important: Do not put spaces around the
=sign. - Values can be text (strings) or numbers.
Let's try assigning some values:
#!/bin/bash
# Assigning a string
MY_NAME="CoddyKit"
# Assigning a number
LESSON_NUMBER=2
echo "Name: $MY_NAME"
echo "Lesson: $LESSON_NUMBER"All lessons in this course
- First Bash Script & Shebang
- Variables and User Input
- Conditional Statements (if, else, elif)
- Working with Arrays in Bash