Introduction
Variables let you save information so you can use it again later without typing it over and over.
Variables let you save information so you can use it again later without typing it over and over.
variable_name=value
# To use the value: $variable_nameDo not put spaces around the equal sign.
Use $ before the variable name to get its value.
name and then prints a greeting using it.name=Alice
echo "Hello, $name!"count and prints it.count=5 echo "Count is $count"
path=/home/user/docs
echo "Files are in $path"This script shows how you can save a name in a variable and reuse it. Then it changes the name and uses the new one.
#!/bin/bash # Store a name in a variable user=Bob # Use the variable to greet echo "Welcome, $user!" # Change the variable user=Carol # Use it again echo "Now, hello $user!"
Variables in bash are case sensitive: user and User are different.
Always use quotes around variables when printing to avoid word splitting issues.
Variables store data so you can reuse it easily.
Use $variable_name to get the stored value.
Changing a variable updates the value for later use.
name="Alice" echo "Hello, $name!" name="Bob" echo "Hello again, $name!"
count=5 echo "Count is $count" count = 10 echo "New count is $count"
Pass the test and this pattern moves unknown