Q1Which is the correct way to assign to a shell variable?
Shell Script - Variables and Command Substitution
Assign with name=value and reference with "$name", see how single vs double quotes expand differently, run $(date) command substitution, do $(( n * 2 )) arithmetic, and read input — illustrated and practiced in a browser terminal.
Using Variables — Assign and Reference
In a shell script, you give a value a name and reuse it as many times as you like.
You assign with the form name=value, and you must not put spaces around the =.
If you do, it gets read as a separate command and errors out.
You read the assigned value back with $name or ${name}.
If a character follows $name directly, it's treated as part of the variable name, so use ${name} to mark the boundary.
name=alice, then read it out with $name to use it.Quotes — How Single and Double Differ in Expansion
There are two kinds of quotes for wrapping a string, and they expand differently.
Inside single quotes '...', $name is treated literally and isn't expanded.
Inside double quotes "...", $name is replaced by its value.
Use double quotes when you want to drop a value into the middle of text, like "Hello $name", and single quotes when you want to print $ as-is.
$name as-is; double quotes replace it with the value.name=alice # no spaces around =
echo "hello $name" # hello alice (double quotes expand)
echo 'hello $name' # hello $name (single quotes don't)
echo "path is ${name}/data" # use {} to mark the name boundary
Command Substitution — $()
$(command) is a way to use a command's output as a value right where you write it.
Write something like today=$(date +%Y-%m-%d) and the date date produces goes straight into today.
You use it to capture a command's result into a variable or to drop it into the middle of text.
$(date) is replaced by the output of running date, and today=$(date) puts that value into the variable.today=$(date +%Y-%m-%d) # capture date's output into a variable
echo "today is $today" # embed the captured value in text
user=$(whoami) # another command's output can be a value too
echo "user: $user"
Arithmetic Expansion — $(( ))
Inside $(( )) is integer arithmetic.
$(( 3 + 4 )) is replaced by 7.
You can use + to add, - to subtract, * to multiply, / to divide, and % for the remainder.
Variables go in without a $, written plainly by name like $(( n * 2 )).
$(( )) is evaluated: $(( 3 + 4 )) is 7, and with n=5 then $(( n * 2 )) is 10.echo "sum: $(( 10 + 5 ))" # adding gives 15
n=7
echo "double: $(( n * 2 ))" # variables go in without a $
echo "rest: $(( 17 % 5 ))" # remainder with % is 2
Taking Input — read
read v reads one line of input and puts it into the variable v.
You use it when you want to ask the user for a value inside a script.
In this course's console, piping input in, like printf 'value\n' | ./greet.sh, runs reliably.
Printing a prompt with echo before read makes it clearer what to type.
read n reads one line and puts it into n. Piping a value in with printf delivers it to read without typing by hand.echo "enter your name:" # prompt for what to type
read who # read one line into who
echo "hi, $who" # embed the value you got in text
printf 'bob\n' | (read who; echo "hi, $who") # input can come from a pipe too
Embed a Count in a Message with Command Substitution
Command substitution $() is often used to capture a "counted result", like a number of files, into a variable.
Write something like n=$(ls *.txt | wc -l) and the combined result of ls and wc -l goes into n.
Embed that variable in a double-quoted string and you can build a readable one-line message that includes the count.
ls and wc -l goes into n via command substitution, and embedding it in text shows txt files: 2.Knowledge Check
Answer each question one by one.
Q2How does echo '$name' display (when name=alice)?
Q3Which way puts a command's output into a variable as a value?