Code Reference

Getting Started with Bash

Bash · Reference cheat sheet

Getting Started with Bash

Bash · Reference cheat sheet


📋 Overview

Bash (Bourne Again SHell) is a command-line shell and scripting language common on Linux and macOS. You type commands interactively or save them in a script (*.sh) starting with a shebang.

🔧 Core concepts

IdeaMeaning
ShellProgram that reads commands and runs programs
PromptWhere you type ($ often means a normal user)
CommandProgram name + arguments: ls -la
ScriptFile of commands executed in order
Shebang#!/usr/bin/env bash on line 1 of scripts
Exit status0 usually means success; non-zero means failure

On Windows you can use Git Bash, WSL, or another Unix-like environment.

💡 Examples

Who am I / where am I:

whoami
pwd
echo "Hello from Bash"

Check Bash version:

bash --version
echo "$BASH_VERSION"

Minimal script (hello.sh):

#!/usr/bin/env bash
set -euo pipefail
echo "Hello, World!"
chmod +x hello.sh
./hello.sh

Run a one-liner without a file:

bash -c 'echo hi; pwd'

⚠️ Pitfalls

  • Spaces matter: cd My Dir fails; use cd "My Dir".
  • Your login shell might be zsh on macOS — scripts can still use Bash via shebang.
  • Copying Windows paths (C:\...) into Bash needs /c/... or WSL paths.
  • Forgetting chmod +x leads to “Permission denied” on ./script.sh.

On this page