Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Creating a script file (.sh) in Bash Scripting - Interactive Practice

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

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

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

Complete the command to make the script file executable.

Bash Scripting
chmod [1] myscript.sh
Drag options to blanks, or click blank then click option'
A-r
B+x
C+r
D-w
Attempts:
3 left
💡 Hint
Common Mistakes
Using -r or -w which remove read or write permissions.
Using +r which only adds read permission, not execute.
3fill in blank
hard

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

Bash Scripting
echo "Current directory is: [1]"
Drag options to blanks, or click blank then click option'
A`PWD`
BPWD
C$PWD()
D$PWD
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the $ sign before PWD.
Using PWD as a command instead of a variable.
4fill in blank
hard

Fill both blanks to create a script that prints all arguments passed to it.

Bash Scripting
echo "Arguments: [1]"
for arg in [2]; do
  echo "$arg"
done
Drag options to blanks, or click blank then click option'
A"$@"
B$*
C"$*"
D$@
Attempts:
3 left
💡 Hint
Common Mistakes
Using $* which treats all arguments as one string.
Quoting $@ in the loop which breaks iteration.
5fill in blank
hard

Fill all three blanks to create a script that checks if a file exists and prints a message.

Bash Scripting
if [[ [1] [2] [3] ]]; then
  echo "File exists."
else
  echo "File not found."
fi
Drag options to blanks, or click blank then click option'
A-f
Bmyfile.txt
C-d
Dfile.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Using -d which checks for directories, not files.
Using the wrong filename or missing quotes if needed.

Practice

(1/5)
1. What is the main purpose of creating a .sh script file in bash?
easy
A. To compile programs
B. To store text documents
C. To save a list of commands to run them later easily
D. To create image files

Solution

  1. Step 1: Understand what a script file is

    A .sh file contains bash commands saved to run multiple times without typing again.
  2. Step 2: Identify the purpose of saving commands

    Saving commands helps automate tasks and avoid retyping, which matches To save a list of commands to run them later easily.
  3. Final Answer:

    To save a list of commands to run them later easily -> Option C
  4. Quick Check:

    Script file purpose = save commands [OK]
Hint: Scripts save commands to run anytime without retyping [OK]
Common Mistakes:
  • Thinking .sh files are for text or images
  • Confusing scripts with compiled programs
  • Not knowing scripts automate commands
2. Which line should you add at the very top of a bash script file to specify the interpreter?
easy
A. #!/bin/python
B. #!/bin/bash
C. #!/usr/bin/node
D. #!/bin/shutdown

Solution

  1. Step 1: Identify the correct shebang for bash scripts

    The shebang line tells the system which program runs the script. For bash, it is #!/bin/bash.
  2. Step 2: Eliminate incorrect interpreters

    Options A, C, and D point to other interpreters or invalid commands.
  3. Final Answer:

    #!/bin/bash -> Option B
  4. Quick Check:

    Shebang for bash = #!/bin/bash [OK]
Hint: Use #!/bin/bash as first line to run bash scripts [OK]
Common Mistakes:
  • Using wrong interpreter path
  • Forgetting the #! at the start
  • Confusing bash with other languages
3. What will be the output when you run this script named greet.sh?
#!/bin/bash
echo "Hello, $USER!"
medium
A. Syntax error
B. Hello, root!
C. Hello, $USER!
D. Hello, your-username!

Solution

  1. Step 1: Understand the variable $USER in bash

    $USER is an environment variable that holds the current user's username.
  2. Step 2: Predict the echo output

    The echo command prints "Hello, " followed by the username, so it outputs "Hello, your-username!" where your-username is the actual user.
  3. Final Answer:

    Hello, your-username! -> Option D
  4. Quick Check:

    $USER expands to username = Hello, your-username! [OK]
Hint: Variables like $USER show your username in scripts [OK]
Common Mistakes:
  • Thinking $USER prints literally
  • Expecting 'root' always
  • Confusing syntax causing errors
4. You wrote a script myscript.sh but when you run ./myscript.sh, it says Permission denied. What is the likely fix?
medium
A. Run chmod +x myscript.sh to make it executable
B. Add #!/bin/bash at the top
C. Rename the file to myscript.txt
D. Run the script with bash myscript.sh only

Solution

  1. Step 1: Understand the cause of 'Permission denied'

    This error usually means the file is not executable by the user.
  2. Step 2: Make the script executable

    Using chmod +x myscript.sh adds execute permission, allowing ./myscript.sh to run.
  3. Final Answer:

    Run chmod +x myscript.sh to make it executable -> Option A
  4. Quick Check:

    Permission denied fix = chmod +x [OK]
Hint: Use chmod +x to allow script execution [OK]
Common Mistakes:
  • Forgetting to make script executable
  • Only adding shebang without permissions
  • Renaming file instead of fixing permissions
5. You want to create a script backup.sh that prints "Backup started" and then lists files in the current directory. Which script content is correct?
hard
A. #!/bin/bash echo "Backup started" ls
B. echo "Backup started" ls #!/bin/bash
C. #!/bin/bash echo Backup started ls-l
D. #!/bin/sh echo "Backup started" list

Solution

  1. Step 1: Check the shebang and commands order

    The script must start with #!/bin/bash to run in bash, then commands follow in order.
  2. Step 2: Verify commands and syntax

    #!/bin/bash echo "Backup started" ls has correct shebang, echo with quotes, and ls command to list files. Others have wrong order, missing quotes, or invalid commands.
  3. Final Answer:

    #!/bin/bash echo "Backup started" ls -> Option A
  4. Quick Check:

    Correct script structure = #!/bin/bash echo "Backup started" ls [OK]
Hint: Start script with #!/bin/bash then commands line by line [OK]
Common Mistakes:
  • Placing shebang after commands
  • Using wrong commands like 'list'
  • Missing quotes around strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard