Learn by reading through in order

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.

From assignment to reference
name=alicename gets the valueecho "$name"$name replaced by valueprints aliceassignmentreference
Put a value in with 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.

Single vs double quote expansion
echo '$name'prints $name (no expansion)echo "$name"prints alice (expanded)
Single quotes print $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

Create a script that puts a username in a variable and shows how the display changes depending on the quotes.

① Open quotes.sh with vi quotes.sh, press i to enter insert mode, and write #!/bin/sh on the first line.

② In the script, assign a name to a variable, then print one line with double quotes ("greeting + variable") and one line with single quotes in the same form (you can copy what to write from the answer panel and paste it).

③ Press Esc, save and quit with :wq, then give the file execute permission.

④ Run the script and confirm the double-quote and single-quote lines display differently. (If it runs correctly, the explanation appears.)

Linux console
0 / 3 completed
Loading Linux Terminal...

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.

How command substitution flows
$(date)2026-06-18today=$(date)replaced by outputtoday=2026-06-18run datecommand substitutionassignment
$(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"

Create a script that captures a command's output into a variable with command substitution $() and embeds it in a message.

① Open datestamp.sh with vi datestamp.sh, press i to enter insert mode, and write #!/bin/sh on the first line.

② In the script, capture the output of date into a variable with command substitution $(), then embed that variable in a double-quoted string and print it on one line (you can copy what to write from the answer panel and paste it).

③ Press Esc, save and quit with :wq, then give the file execute permission.

④ Run the script and confirm a message containing the date is shown.

Linux console
0 / 3 completed
Loading Linux Terminal...

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 )).

How arithmetic expansion works
$(( 3 + 4 ))evaluate= 7n=5$(( n * 2 ))= 10
The expression inside $(( )) 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

Create a script that does integer arithmetic with arithmetic expansion $(( )).

① Open calc.sh with vi calc.sh, press i to enter insert mode, and write #!/bin/sh on the first line.

② In the script, assign two numbers to variables, use $(( )) to do several calculations such as the sum, product, and remainder, and print the results on one line (you can copy what to write from the answer panel and paste it).

③ Press Esc, save and quit with :wq, then give the file execute permission.

④ Run the script and confirm the calculated results are shown.

Linux console
0 / 3 completed
Loading Linux Terminal...

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.

Taking input with read
echo "enter:"show the promptwait for inputpipe a value inread nn=7
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

Create a script that reads one line of input with read and embeds it in a greeting.

① Open greet.sh with vi greet.sh, press i to enter insert mode, and write #!/bin/sh on the first line.

② In the script, show a prompt with echo, read one line for the name with read, then embed that name in a double-quoted greeting and print it (you can copy what to write from the answer panel and paste it).

③ Press Esc, save and quit with :wq, then give the file execute permission.

④ Run the script with the input piped in, and confirm a greeting containing the name you entered is shown.

Linux console
0 / 3 completed
Loading Linux Terminal...

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.

Embed a count in a message
ls vdemo/*.txtcount = 2n=$( ... )embed $n in texttxt files: 2count with wc -lcount themembed in message
The count from ls and wc -l goes into n via command substitution, and embedding it in text shows txt files: 2.

Create a script that takes the number of text files into a variable with command substitution and shows it embedded in a message.

① As material to count, make a working directory with mkdir -p vdemo and create two .txt files, like printf 'a\n' > vdemo/a.txt.

② Open count-txt.sh with vi count-txt.sh, press i to enter insert mode, and write #!/bin/sh on the first line.

③ In the script, take the number of .txt files in vdemo into a variable with command substitution, then embed that variable in a double-quoted string and show a message with the count (you can copy what to write from the answer panel and paste it).

④ Press Esc, save with :wq, give it execute permission, then run it and confirm a message containing the count is shown.

Linux console
0 / 3 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1Which is the correct way to assign to a shell variable?

Q2How does echo '$name' display (when name=alice)?

Q3Which way puts a command's output into a variable as a value?