📖 Scenario: You want to create a simple Bash script that greets a user by name. This is like writing a short note to a friend, but using a computer script.
🎯 Goal: Build a Bash script that stores a name in a variable, stores a greeting message, combines them, and then prints the full greeting.
📋 What You'll Learn
Create a variable called name with the value "Alice"
Create a variable called greeting with the value "Hello"
Create a variable called message that combines greeting and name with a space in between
Print the message variable to the terminal
💡 Why This Matters
🌍 Real World
Bash scripts like this are used to automate simple tasks like sending greetings, setting up environments, or running commands with variables.
💼 Career
Knowing how to write basic Bash scripts is useful for system administrators, developers, and anyone working with Linux or Unix systems.
Progress0 / 4 steps
1
Create the name variable
Create a variable called name and set it to the string "Alice".
Bash Scripting
Hint
Use name="Alice" to create the variable.
2
Create the greeting variable
Add a variable called greeting and set it to the string "Hello".
Bash Scripting
Hint
Use greeting="Hello" to create the variable.
3
Combine greeting and name into message
Create a variable called message that combines greeting and name with a space between them. Use message="$greeting $name".
Bash Scripting
Hint
Use message="$greeting $name" to combine the variables.
4
Print the message
Print the message variable to the terminal using echo.
Bash Scripting
Hint
Use echo "$message" to print the message.
Practice
(1/5)
1. What is the purpose of the first line #!/bin/bash in a Bash script?
easy
A. It comments out the first line so it is ignored.
B. It prints a welcome message when the script runs.
C. It sets the script to run in the background automatically.
D. It tells the system to use the Bash shell to run the script.
Solution
Step 1: Understand the shebang line
The line #!/bin/bash is called a shebang and tells the system which interpreter to use.
Step 2: Identify its role in script execution
This line ensures the script runs with the Bash shell, not another shell or program.
Final Answer:
It tells the system to use the Bash shell to run the script. -> Option D
Quick Check:
Shebang = Bash interpreter [OK]
Hint: Shebang line always points to the shell interpreter [OK]
Common Mistakes:
Thinking it prints a message
Confusing it with a comment
Assuming it runs script in background
2. Which of the following is the correct way to print "Hello, World!" in a Bash script?
easy
A. echo "Hello, World!"
B. printf(Hello, World!)
C. console.log("Hello, World!")
D. print("Hello, World!")
Solution
Step 1: Identify the Bash command for output
In Bash, echo is used to print text to the screen.
Step 2: Check syntax correctness
echo "Hello, World!" correctly prints the message with quotes around the string.
Final Answer:
echo "Hello, World!" -> Option A
Quick Check:
Print command in Bash = echo [OK]
Hint: Use echo to print text in Bash scripts [OK]
Common Mistakes:
Using print() like in Python
Using console.log() from JavaScript
Missing quotes in printf
3. What will be the output of this Bash script?
#!/bin/bash
echo "Start"
echo "End"
medium
A. Start\nEnd
B. Start
End (two lines)
C. Start
End
D. Start End
Solution
Step 1: Understand echo behavior
The echo command prints text followed by a new line by default.
Step 2: Analyze the script output
Two echo commands print "Start" and "End" each on its own line.
Final Answer:
Start
End (two lines) -> Option B
Quick Check:
echo adds newline = two lines output [OK]
Hint: Each echo prints on a new line by default [OK]
Common Mistakes:
Thinking echo prints on same line
Confusing \n as literal text
Ignoring new line after echo
4. Identify the error in this Bash script:
#!/bin/bash
echo Hello World
medium
A. Missing quotes around Hello World
B. echo command is misspelled
C. No error, script runs fine
D. Missing shebang line
Solution
Step 1: Check script syntax
The script has a correct shebang line and uses echo properly.
Step 2: Understand echo argument rules
Quotes are optional if no special characters or spaces that confuse shell; here it works fine.
Final Answer:
No error, script runs fine -> Option C
Quick Check:
echo without quotes works here [OK]
Hint: Quotes optional if no special chars or spaces [OK]
Common Mistakes:
Assuming quotes are always required
Thinking shebang is missing
Believing echo is misspelled
5. You want to write a Bash script that prints your name and then the current date. Which script below will do this correctly?
hard
A. #!/bin/bash
echo "My name is Alice"
date
B. #!/bin/bash
print "My name is Alice"
date
C. #!/bin/bash
echo My name is Alice
current_date
D. #!/bin/bash
echo "My name is Alice"; date %Y-%m-%d
Solution
Step 1: Check commands for printing text and date
echo prints text; date prints current date and time.
Step 2: Verify script correctness
#!/bin/bash
echo "My name is Alice"
date uses echo correctly and calls date without errors.
Final Answer:
#!/bin/bash\necho "My name is Alice"\ndate -> Option A
Quick Check:
Use echo and date commands correctly [OK]
Hint: Use echo for text and date command for current date [OK]
Common Mistakes:
Using print instead of echo
Calling undefined commands like current_date
Incorrect date command syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.