📖 Scenario: Imagine you want to tell your computer to do a few simple tasks automatically, like saying hello and showing the date. Instead of typing commands one by one, you can write them down in a file and run it all at once. This file is called a shell script.
🎯 Goal: You will create a simple shell script that prints a greeting and shows the current date.
📋 What You'll Learn
Create a shell script file with the correct first line (shebang).
Write commands inside the script to print a greeting message.
Write a command to display the current date.
Run the script to see the output.
💡 Why This Matters
🌍 Real World
Shell scripts help automate repetitive tasks on your computer, saving time and effort.
💼 Career
Knowing how to write and run shell scripts is useful for system administrators, developers, and anyone working with computers.
Progress0 / 4 steps
1
Create the shell script file with shebang
Create a file named greet.sh and add the first line #!/bin/bash to tell the computer this is a shell script.
Bash Scripting
Hint
The first line #!/bin/bash tells the system to use the bash shell to run the script.
2
Add a greeting message
Add a line to the greet.sh script that prints Hello, friend! using the echo command.
Bash Scripting
Hint
Use echo followed by the message in quotes to print text.
3
Add a command to show the current date
Add a line to the greet.sh script that runs the date command to show the current date and time.
Bash Scripting
Hint
The date command shows the current date and time.
4
Run the shell script and see the output
Run the greet.sh script in the terminal using bash greet.sh and observe the output.
Bash Scripting
Hint
Use the command bash greet.sh to run your script and see the greeting and date printed.
Practice
(1/5)
1. What is a shell script in simple terms?
easy
A. A file containing commands that run automatically in the shell
B. A program that only runs on Windows
C. A type of hardware device
D. A graphical user interface for the computer
Solution
Step 1: Understand what a shell script is
A shell script is a text file with commands that the shell can run automatically.
Step 2: Compare options to definition
Only A file containing commands that run automatically in the shell describes a file with commands running automatically in the shell.
Final Answer:
A file containing commands that run automatically in the shell -> Option A
Quick Check:
Shell script = file with commands [OK]
Hint: Think: script means commands saved to run later [OK]
Common Mistakes:
Confusing shell script with software programs
Thinking shell scripts are hardware
Believing shell scripts are graphical tools
2. Which of these is the correct way to start a shell script file?
easy
A.
B. #!/bin/bash
C. print('Hello World')
D. function main() {}
Solution
Step 1: Identify the shell script shebang
The first line in a shell script usually starts with #!/bin/bash to tell the system to use bash shell.
Step 2: Check other options
<!DOCTYPE html> is for HTML, print('Hello World') is Python, function main() {} is like JavaScript. None are shell script shebangs.
Final Answer:
#!/bin/bash -> Option B
Quick Check:
Shell scripts start with #!/bin/bash [OK]
Hint: Look for #! to start shell scripts [OK]
Common Mistakes:
Using code from other languages as shell script start
Omitting the shebang line
Confusing shell script with HTML or Python
3. What will this shell script output?
#!/bin/bash
echo "Hello, World!"
medium
A. Error: command not found
B. echo "Hello, World!"
C. No output
D. Hello, World!
Solution
Step 1: Understand the echo command
The echo command prints the text inside quotes to the screen.
Step 2: Predict the output
The script will print exactly: Hello, World!
Final Answer:
Hello, World! -> Option D
Quick Check:
echo prints text = Hello, World! [OK]
Hint: echo prints text exactly as given [OK]
Common Mistakes:
Thinking echo prints the command itself
Expecting error without cause
Assuming no output without running script
4. This shell script is supposed to print "Done" but it shows an error:
#!/bin/bash
echo Done
What is the likely problem?
medium
A. The shebang line is incorrect
B. The echo command is misspelled
C. The script file is missing execute permission
D. The script needs a semicolon after echo
Solution
Step 1: Check script permissions
If the script shows an error when run, it might not have execute permission set.
Step 2: Verify other options
Echo is correct, shebang is correct, and semicolon is not needed after echo.
Final Answer:
The script file is missing execute permission -> Option C
Quick Check:
Missing execute permission causes errors [OK]
Hint: Make script executable with chmod +x [OK]
Common Mistakes:
Assuming syntax error without checking permissions
Adding unnecessary semicolons
Changing correct shebang line
5. You want to automate cleaning temporary files daily using a shell script. Which statement best describes what you should do?
hard
A. Write a shell script with commands to delete temp files, then schedule it with cron
B. Manually type delete commands every day in the terminal
C. Use a graphical tool only, no scripts needed
D. Write a shell script but run it only once manually
Solution
Step 1: Understand automation goal
To automate daily cleaning, you need a script that runs commands automatically.
Step 2: Use scheduling tool
Cron is a tool that runs scripts on a schedule, perfect for daily tasks.
Final Answer:
Write a shell script with commands to delete temp files, then schedule it with cron -> Option A
Quick Check:
Script + cron = daily automation [OK]
Hint: Combine script with cron for daily tasks [OK]
Common Mistakes:
Thinking manual typing is automation
Ignoring scheduling tools like cron
Running script only once instead of daily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.