Bash Tutorial · Bash Basics · Lesson 2 of 30
Bash Get Started
Open a terminal, check your Bash version, and write and run your first script with a shebang line.
Open a terminal
- Linux: open "Terminal"; Bash is usually the default shell.
- macOS: open Terminal; the default shell is zsh, and
bashstarts Bash (macOS ships an old 3.2; install a current one with Homebrew:brew install bash). - Windows: install WSL (
wsl --install) and open Ubuntu.
Check the version with bash --version.
Your first script
Create hello.sh:
#!/usr/bin/env bash
echo "Hello from a script"
The first line, the shebang, tells the system which interpreter runs the file. Make it executable and run it:
chmod +x hello.sh
./hello.sh
The example below creates a script, makes it executable and runs it, all from Bash:
cat > hello.sh <<'EOF'
#!/usr/bin/env bash
echo "Hello from a script"
echo "This script is called $0"
EOF
chmod +x hello.sh
./hello.sh
ls -l hello.sh | cut -c1-10
Output
Hello from a script This script is called ./hello.sh -rwxrwxr-x
Run this on your computer — see “Get Started” for setup.
./ is needed because the current directory isn't on your PATH (a safety feature).
Test yourself
What does the shebang line #!/usr/bin/env bash do?
Comments out the first lineTells the system to run the file with bashMakes the file executableImports the bash library
Show answer
Answer: Tells the system to run the file with bash
Without a shebang, ./script runs with whatever shell started it; with it, the right interpreter is always used.