Shell by Example: Hello World POSIX + Bash

Our first shell script is the classic “Hello World”. This example demonstrates the basic structure of a shell script.

You can run this script by saving it to a file named 01-hello-world.sh, making it executable with chmod +x 01-hello-world.sh, and running it via ./01-hello-world.sh in your terminal.

The echo command prints text to standard output. It’s one of the most commonly used commands in shell scripting. You can print multiple lines using multiple echo commands.

Edit
#!/bin/sh
echo "Hello, World!"
echo "Welcome to Shell by Example!"
Output:
Hello, World!
Welcome to Shell by Example!

Bash

The -n flag prevents echo from adding a newline at the end of the output. This is useful when you want to continue on the same line.

Edit
#!/bin/bash
echo -n "Hello"
echo ", World!"
Output:
Hello, World!

Comments »