The smallest Bash program is often a one-liner using echo. Unlike Python print(), the shell expands variables before external commands run.
One-liner
echo "Hello, World"echo writes arguments to stdout, separated by spaces, followed by a newline.
Script file
#!/usr/bin/env bash
echo "Hello, World"Save as hello.sh, make executable, run:
chmod +x hello.sh
./hello.sh
Shebang explained
#!/usr/bin/env bash finds bash on PATH—more portable than hard-coding /bin/bash on mixed systems.
Important interview questions and answers
- Q: What does echo do?
A: Writes arguments to standard output; the shell performs variable expansion first. - Q: Why chmod +x?
A: The kernel needs execute permission to run a file as a program (when invoked by path).
Self-check
- Which command prints text in these examples?
- What does the shebang line tell the OS?
Challenge
Hello script locally
- Save the shebang example as
hello.sh. - Run
chmod +x hello.shthen./hello.sh. - Also try
bash hello.shwithout execute bit.
Done when: both invocation styles print Hello, World.
Interview prep
- echo vs printf?
echo is simple; printf offers finer format control for scripts.
- chmod +x why?
Grants execute permission so ./script can run the shebang interpreter.