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
- Create Project Directory
1 2
mkdir hello-world-bash cd hello-world-bash
- Initialize Project Files
1 2
touch hello_world.sh touch .devcontainer/devcontainer.json
Configuring Dev Container
- 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
- 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!"
- Make Script Executable
1
chmod +x hello_world.sh
Running the Script
- 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 -efor strict error handling - Implement input validation
- Use functions for modularity
Recommended VS Code Extensions
- 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.