Shell Script
22%
Course progress
1 of 9 topics done
Keep going, you are close!
2 topics in a row
1 of 9 topics done
LessonΒ·2 of 9

Basic Script

N/A9 lessons
1

HelloWorld

πŸ‘‹ Hello World - Your First Shell Script

BashFirst ScriptEcho CommandExecutionLevelDuration

πŸ“‹ Overview

🎯 Your First Shell Script - Hello World

This guide walks you through creating and executing your first shell script, introducing the echo command and script execution basics.

πŸ” What This Guide Covers

  • πŸ“ Script Creation - Creating your first shell script file
  • πŸ”§ Shebang - Understanding the script header
  • πŸ“’ Echo Command - Printing output to the terminal
  • 🎨 Colored Output - Adding colors to your output
  • πŸ” Permissions - Making scripts executable
  • ▢️ Execution - Running your first script

πŸ“ Creating Your First Script

Script CreationGetting Started

Step 1: Create the Script File

Create a new file named helloworld.sh:

bash
vi helloworld.sh

Or using other editors:

bash
nano helloworld.sh
# or
cat > helloworld.sh << 'EOF'
EOF

πŸ”§ Basic Hello World Script

Basic ScriptSimple Example

Step 2: Write the Script

Add the following content to your helloworld.sh file:

bash
#!/bin/bash
echo "Hello World"

Explanation:

  • #!/bin/bash - Shebang directive specifying the Bash interpreter
  • echo - Command to print text to the terminal
  • "Hello World" - The text to be printed

🎨 Enhanced Hello World with Colors

Advanced OutputColor Codes

Extended Script with Multiple Outputs

bash
#!/bin/bash
echo "Hello World"
echo 'This is our first basic shell script'
echo -e "\033[0;31m ❌ Fail message"
echo -e "\033[0;32m βœ… Success message"
echo -e "\033[0;33m ⚠️  Warning message here"

Color Code Reference:

CodeColorUsage
\033[0;31mRedError/Fail messages
\033[0;32mGreenSuccess messages
\033[0;33mYellowWarning messages
\033[0;34mBlueInformation
\033[0mResetReset to default color

Note: Use echo -e flag to enable interpretation of backslash escapes.


πŸ” Making the Script Executable

PermissionsAccess Control

Step 3: Add Execute Permission

Before running the script, make it executable:

bash
chmod +x helloworld.sh

Explanation:

  • chmod - Change file mode/permissions
  • +x - Add execute permission
  • helloworld.sh - The script file

▢️ Running Your Script

ExecutionRunning Scripts

Step 4: Execute the Script

Run your script using one of these methods:

Method 1: Direct Execution (with shebang)

bash
./helloworld.sh

Method 2: Explicit Interpreter

bash
bash helloworld.sh

Method 3: Source Command

bash
source helloworld.sh
# or
. helloworld.sh

πŸ“Š Expected Output

OutputResults

Basic Script Output

text
Hello World

Enhanced Script Output

text
Hello World
This is our first basic shell script
❌ Fail message
βœ… Success message
⚠️  Warning message here

πŸ’‘ Key Concepts

ConceptsLearning Points
ConceptDescription
Shebang#!/bin/bash tells the system to use Bash interpreter
EchoPrints text to standard output (terminal)
Permissionschmod +x makes the file executable
Execution./script.sh runs the script from current directory
Color CodesANSI escape sequences for colored output

βœ… Best Practices

Best PracticesStandards
  • Always include a shebang at the beginning
  • Use meaningful script names with .sh extension
  • Make scripts executable with chmod +x
  • Add comments to explain what your script does
  • Test scripts before using them in production
  • Use echo for simple output, printf for complex formatting

πŸ”— Navigation

Navigation

⬅️ Previous: Module 1: Shebang and Execution

πŸ“ Current Location: Hello World - Your First Script (You are here)

➑️ Next Step: Comments and Documentation

πŸ—ΊοΈ Full Path: Introduction β†’ Overview β†’ Module 2: Basics β†’ Hello World


πŸ“š Additional Resources

ResourcesDocumentation

Hello World Complete

πŸŽ‰ Congratulations! Your First Script is Complete! πŸŽ‰

➑️ Continue to Comments and Documentation