A variable in bash is a named container for a string. Bash treats everything as a string by default. Numbers, paths, booleans, all strings until you tell bash otherwise.
name=alice
count=5
message="hello world"
Three rules that trip up almost every beginner:
=. name = alice is wrong. Bash reads that as "run the command name with arguments = and alice".msg=hello world fails, msg="hello world" works. More on quoting next.PATH, HOME), lowercase for local script variables. This isn't enforced by bash, it's how you avoid clobbering system variables by accident.Use a variable's value with $:
name=alice
echo $name # alice
echo "hello, $name" # hello, alice
The longer form ${name} does the same thing but with explicit
boundaries. You need it when the variable name butts up against
other characters:
file=report
echo $file_final # wrong: bash looks for a variable called "file_final"
echo ${file}_final # right: prints "report_final"
You don't declare a variable before using it. Assign, then use.
first_name=Alice
last_name=Smith
full_name="$first_name $last_name"
echo "$full_name"
Four lines, no boilerplate, and the shape of every script you'll ever write.