Post

Hello World Bash Script in Dev Container A Comprehensive Guide

Hello World Bash Script in Dev Container A Comprehensive Guide

Prerequisites

1. Development Environment Setup

  • Docker: Installed on your system
  • Visual Studio Code: With Remote - Containers extension
  • Git: Installed (optional, but recommended)

2. Required Tools

  • Docker
  • VS Code
  • Remote - Containers extension

Step-by-Step Guide

Creating the Project Structure

  1. Create Project Directory
    1
    2
    
    mkdir hello-world-bash
    cd hello-world-bash
    
  2. Initialize Project Files
    1
    2
    
    touch hello_world.sh
    touch .devcontainer/devcontainer.json
    

Configuring Dev Container

  1. Dev Container Configuration (.devcontainer/devcontainer.json)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    {
      "name": "Bash Development Container",
      "image": "ubuntu:latest",
      "customizations": {
        "vscode": {
          "extensions": [
            "shakram02.bash-snippets"
          ]
        }
      },
      "postCreateCommand": "apt-get update && apt-get install -y bash",
      "remoteUser": "root"
    }
    

Writing the Bash Script

  1. Create Hello World Script (hello_world.sh)
    1
    2
    3
    4
    
    #!/bin/bash
    
    # Simple Hello World Script
    echo "Hello, World from Bash in Dev Container!"
    
  2. Make Script Executable
    1
    
    chmod +x hello_world.sh
    

Running the Script

  1. Execute the Script
    1
    
    ./hello_world.sh
    

Troubleshooting

Common Issues

  • Ensure script has executable permissions
  • Check line endings (use LF, not CRLF)
  • Verify Docker is running
  • Confirm VS Code Remote Containers extension is installed

Potential Error Resolution

1
2
3
4
5
# If permission denied
sudo chmod +x hello_world.sh

# If line ending issues
dos2unix hello_world.sh

Best Practices

  • Always use shebang (#!/bin/bash)
  • Add error handling
  • Use meaningful variable names
  • Comment your code

Advanced Tips

  • Consider using set -e for strict error handling
  • Implement input validation
  • Use functions for modularity
  • Bash Debug
  • Bash Snippets
  • ShellCheck (Linting)

Resources

License

MIT License - Feel free to use and modify this guide ```

Contributing

Contributions welcome! Open an issue or submit a pull request.

This post is licensed under CC BY 4.0 by the author.