Bash Exercises
Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.
Bash Introduction
Exercise
Print text.
"Hi"Running Scripts
Exercise
Make a file executable.
chmod hello.sh
Shell Basics
Exercise
List files.
Comments and echo
Exercise
Print without a newline.
echo "x"
Variables
Exercise
Read a variable.
echo name
Conditions
Exercise
Close an if block.
if true; then echo a; Loops
Exercise
Start a loop.
i in 1 2 3; do echo $i; doneQuoting
Exercise
Print the name with expansion.
echo $name
Special Variables
Exercise
Number of arguments.
echo #
Parameter Expansion
Exercise
Default value.
echo "${xfallback}"
Arithmetic
Exercise
Integer math syntax.
echo $(1 + 2))
Reading Input
Exercise
Prompt option.
read "Name: " n
Exit Status and Chaining
Exercise
Run only if the first worked.
cmd1 cmd2
echo and printf
Exercise
Skip the newline.
echo "hi"
Variables
Exercise
Read a variable.
echo name
Quoting
Exercise
Prevent word splitting.
echo "x"
Command Substitution
Exercise
Capture output.
x=(date)
Arithmetic
Exercise
Integer math.
echo $((a b))
String Length and Slices
Exercise
Length.
echo ${s}
Default Values
Exercise
Use a default.
${xdefault}
Replace and Trim in Variables
Exercise
Remove a suffix.
${f.txt}
Upper and Lower Case
Exercise
Upper case.
echo ${n}
Reading Input
Exercise
Read a line.
-r lineExit Codes
Exercise
Last exit code.
echo $Comments and the Shebang
Exercise
Start a comment.
noteSequences with seq and braces
Exercise
Number sequence.
1 5Formatting Dates
Exercise
Year-month-day.
date +%Comparing Numbers
Exercise
Greater than.
[[ 3 2 ]]
&& and || Chaining
Exercise
Run next only on success.
true echo yes
Printing Tables with printf
Exercise
Left align width 8.
"%s"
Array Slices and Lengths
Exercise
Slice 2 items.
${a[@]:1:}
Sorting and Comparing Strings
Exercise
Not equal strings.
[[ a b ]]
Subshells
Exercise
Group in a subshell.
x=2 )Getting Help
Exercise
Find command type.
echoString Operations
Exercise
Uppercase a variable.
echo "${s}"
Here Documents and Here Strings
Exercise
Feed several lines.
cat <<Wildcards and Brace Expansion
Exercise
Match any text.
ls .txt
Here Documents and Here Strings
Exercise
Feed a string.
wc -w "a b"
tr, cut and paste
Exercise
Select a field.
cut -d: 2
sort and uniq
Exercise
Count duplicates.
uniq grep Patterns
Exercise
Invert.
grep pat
sed Basics
Exercise
Replace all on a line.
sed "s/a/b/"
awk Basics
Exercise
Field separator.
awk -: '{print $1}'
wc, head and tail
Exercise
Last lines.
-n 2 fTest Operators
Exercise
Test for an empty string.
[[ $s ]]
Case Statements
Exercise
End a case block.
case $x in a) ;; More Loops
Exercise
Repeat until true.
[[ $n -ge 3 ]]; do ((n++)); doneselect Menus
Exercise
Menu keyword.
opt in a b c; do break; doneif, elif and else
Exercise
End an if.
if true; then echo; Testing Strings and Numbers
Exercise
Test empty.
[[ $s ]]
Testing Files
Exercise
Directory test.
[[ /tmp ]]
case Statements
Exercise
Default pattern.
) echo other ;;for Loops
Exercise
Range with step.
{1..102}
while, until and break
Exercise
Opposite of while.
cond; do :; doneReading Files Line by Line
Exercise
Keep spaces.
while = read -r l
select Menus
Exercise
Menu keyword.
c in a b; doIndexed Arrays
Exercise
Count the items.
echo ${arr[@]}
Associative Arrays
Exercise
Declare a map.
declare m
Functions In Depth
Exercise
Limit a variable to the function.
n=1Scope and Recursion
Exercise
Make a variable local.
local =1
Arrays
Exercise
Count items.
echo ${a[@]}
Associative Arrays
Exercise
Declare associative.
declare m
Functions
Exercise
Define a function.
hi { echo hi; }
local Variables
Exercise
Function only variable.
x=1Return Values and Status
Exercise
Exit a function.
0Script Arguments
Exercise
Count args.
echo $Defaults and Validation
Exercise
Default for $1.
${1world}
Arrays and Functions
Exercise
First argument inside a function.
echo 1
Pipes and Text Tools
Exercise
Which command counts lines, words or bytes?
ls | -l
Script Arguments and getopts
Exercise
Drop the first argument.
Strict Mode and Safety
Exercise
Exit on any error.
set e
Traps and Signals
Exercise
Run code on exit.
trap 'echo bye' Subshells and Command Substitution
Exercise
Capture output.
now=$date)
Debugging Scripts
Exercise
Trace commands.
bash script.sh
Working with Files
Exercise
Create a temp folder.
mktemp Redirection
Exercise
Redirect errors.
cmd err.log
Pipes and Pipelines
Exercise
Write to a file and continue.
cmd | out.txt | next
Processes and Jobs
Exercise
Run in the background.
sleep 5 Environment Variables
Exercise
Share with child programs.
NAME=valuePermissions
Exercise
Read, write, execute for owner only.
chmod file
Files and Directories
Exercise
Make parents.
mkdir a/b/c
Redirection
Exercise
Append.
echo x file
Pipes and tee
Exercise
Split output.
cmd | file
find
Exercise
By name.
find . - "*.txt"
xargs
Exercise
One per call.
xargs 1 echo
Globbing and Brace Expansion
Exercise
One character.
ls a.txt
Environment Variables
Exercise
Share with children.
FOOJobs and Processes
Exercise
Run in background.
sleep 1 trap
Exercise
Run at exit.
trap f Safer Scripts
Exercise
Fail on unset.
set u
Practice: Backup Names
Exercise
Pad with zeros.
printf "%d" 7
Practice: Count Words
Exercise
Count unique words.
sort | uniq Practice: Simple Menu Script
Exercise
Print status.
case $1 in ) echo go ;; esac
grep Searches Text
Exercise
Ignore case.
grep word file
sed Edits Text
Exercise
Replace every match.
sed "s/a/b/"
awk Processes Columns
Exercise
First field.
awk '{ print }'
sort, uniq, cut, tr, wc
Exercise
Reverse sort numerically.
sort -find and xargs
Exercise
Search for directories.
find . -type JSON and Web Requests
Exercise
Filter JSON.
echo "$j" | ".name"
Process Substitution and Parallel Work
Premium exercise. See plans
Regular Expressions in Bash
Premium exercise. See plans
Parsing Arguments Robustly
Premium exercise. See plans
Error Handling Patterns
Premium exercise. See plans
Performance Tips
Premium exercise. See plans
Project: Backup Script
Premium exercise. See plans
Project: Log Analyser
Premium exercise. See plans
Cron and Scheduling
Premium exercise. See plans
Aliases, Functions and Dotfiles
Premium exercise. See plans
Shell Security
Premium exercise. See plans
Bash Best Practices
Premium exercise. See plans
Practice Projects
Exercise
Check scripts with.