1.3 KiB
1.3 KiB
🧮 Bash Variables
In Bash scripting, variables are used to store values like text or numbers. They allow scripts to be dynamic and reusable.
✍️ Defining Variables
Variables are created without spaces around the = sign:
user="ali"
age=21
useris assigned the value"ali".ageis assigned the value21.
✅ Tip: No
letorvaris needed like in other programming languages.
📢 Using Variables
You can access variables by prefixing them with $:
user="radin"
echo "Welcome, $user"
Output:
Welcome, radin
🧪 Full Script Example
Here's a complete Bash script using variables:
#!/bin/bash
user="mmd"
age=25
echo "$user is $age years old."
Output:
mmd is 25 years old.
📌 Notes
- Variable names are case-sensitive (
Useranduserare different). - Avoid spaces around
=when assigning. - Enclose variable values in quotes if they contain spaces.
✅ Summary
| Task | Syntax |
|---|---|
| Define variable | name="value" |
| Use variable | $name |
| Print value | echo "$name" |
| With script | Use #!/bin/bash at the top |