0
0
Bash Scriptingscripting~10 mins

What a shell script is in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a shell script with the correct shebang line.

Bash Scripting
#![1]
echo "Hello, world!"
Drag options to blanks, or click blank then click option'
A/usr/bin/python
B/usr/bin/node
C/bin/shutdown
D/bin/bash
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong interpreter path like /usr/bin/python for a shell script.
Forgetting the #! at the start of the script.
2fill in blank
medium

Complete the code to assign a value to a variable in a shell script.

Bash Scripting
name=[1]
echo "Hello, $name!"
Drag options to blanks, or click blank then click option'
A"Alice"
BAlice
C'Alice'
D$Alice
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a value without quotes can cause errors if the value has spaces.
Using single quotes prevents variable expansion but is okay for plain strings.
3fill in blank
hard

Fix the error in the script to correctly print the current directory.

Bash Scripting
echo "Current directory is: [1]"
Drag options to blanks, or click blank then click option'
Apwd
B$pwd
C$(pwd)
D`pwd`
Attempts:
3 left
💡 Hint
Common Mistakes
Writing pwd without command substitution just prints the word 'pwd'.
Using $pwd tries to expand a variable named pwd which is usually empty.
4fill in blank
hard

Fill both blanks to create a loop that prints numbers 1 to 5.

Bash Scripting
for i in [1]
do
  echo [2]
done
Drag options to blanks, or click blank then click option'
A1 2 3 4 5
B$i
D1..5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1..5 does not create a list in bash.
Printing i without $ prints the letter 'i' instead of the variable value.
5fill in blank
hard

Fill all three blanks to create a conditional that checks if a file exists.

Bash Scripting
if [[ -[1] [2] ]]; then
  echo "File [3] exists."
else
  echo "File does not exist."
fi
Drag options to blanks, or click blank then click option'
Ae
Bmyfile.txt
Cf
Dfile.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Using -f checks if it is a regular file but -e is more general.
Using different filenames in the test and echo causes confusion.