Code Reference

Hello World

Bash · Reference cheat sheet

Hello World

Bash · Reference cheat sheet


📋 Overview

Bash Hello World prints a line to the terminal with echo (or printf). It confirms you can run commands and, optionally, execute a script file.

🔧 Core concepts

PieceRole
echoPrint arguments followed by a newline
printfFormatted print (more predictable)
ShebangSelects the interpreter for scripts
./fileRun a file in the current directory
bash file.shRun a script without the executable bit

Prefer printf when you care about exact formatting; echo is fine for learning.

💡 Examples

Interactive:

echo "Hello, World!"
printf '%s\n' "Hello, World!"

Script file:

#!/usr/bin/env bash
echo "Hello, World!"
bash hello.sh

With a variable:

#!/usr/bin/env bash
name="Ada"
echo "Hello, ${name}!"

Show command success:

echo "Hello, World!"
echo "exit status was $?"

⚠️ Pitfalls

  • echo $name without quotes breaks on spaces — prefer "$name".
  • Some echo flags differ across systems; printf is more portable.
  • Running sh hello.sh may not be Bash if sh is dash.
  • CRLF line endings from Windows editors can break shebang scripts.

On this page