📖 Scenario: You want to automate a simple task on your computer by writing a shell script. This script will greet the user with a friendly message.
🎯 Goal: Create a shell script file named greet.sh that prints a greeting message when run.
📋 What You'll Learn
Create a script file named greet.sh
Add the correct shebang line for bash scripts
Write a command inside the script to print Hello, welcome to scripting!
Make sure the script is executable
Run the script to see the greeting message
💡 Why This Matters
🌍 Real World
Shell scripts automate repetitive tasks like backups, system checks, or starting programs.
💼 Career
Knowing how to write and run shell scripts is essential for system administrators, developers, and anyone working with Linux or Unix systems.
Progress0 / 4 steps
1
Create the script file with the shebang line
Create a file named greet.sh and add the first line #!/bin/bash to tell the system this is a bash script.
Bash Scripting
Hint
The first line in a bash script should be #!/bin/bash to specify the interpreter.
2
Add a greeting message command
Add a line below the shebang in greet.sh that uses echo to print Hello, welcome to scripting!.
Bash Scripting
Hint
Use echo "Your message" to print text in bash scripts.
3
Make the script executable
Use the command chmod +x greet.sh to make the script file executable.
Bash Scripting
Hint
Use chmod +x filename to allow running the script as a program.
4
Run the script and see the output
Run the script by typing ./greet.sh in the terminal and print the output.
Bash Scripting
Hint
Run the script with ./greet.sh and it should print the greeting message.
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
Step 1: Understand what a script file is
A .sh file contains bash commands saved to run multiple times without typing again.
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.
Final Answer:
To save a list of commands to run them later easily -> Option C
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
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.
Step 2: Eliminate incorrect interpreters
Options A, C, and D point to other interpreters or invalid commands.
Final Answer:
#!/bin/bash -> Option B
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
Step 1: Understand the variable $USER in bash
$USER is an environment variable that holds the current user's username.
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.
Final Answer:
Hello, your-username! -> Option D
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
Step 1: Understand the cause of 'Permission denied'
This error usually means the file is not executable by the user.
Step 2: Make the script executable
Using chmod +x myscript.sh adds execute permission, allowing ./myscript.sh to run.
Final Answer:
Run chmod +x myscript.sh to make it executable -> Option A
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
Step 1: Check the shebang and commands order
The script must start with #!/bin/bash to run in bash, then commands follow in order.
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.
Final Answer:
#!/bin/bash
echo "Backup started"
ls -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.