Hello World (first programs) · Beginner · 15 min
Hello World in Bash: a script with arguments and exit codes
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · GNU Bash 5.3 (bash 5.3-2ubuntu1) · ShellCheck 0.11.0
You will write and run a Bash 5.3 script that greets a name from the command line or the keyboard, rejects bad input with its own exit code, runs in strict mode and passes ShellCheck.
On this page
Bash is the shell most Linux servers give you when you log in, and the language you use to tell it what to do. A Bash script is a text file of the same commands you would type at the prompt, run one after another. There is no compiler and no project file: the shell reads the script line by line, expands variables, and starts each program in turn. That model explains most of Bash's surprises, including the ones in this tutorial.
You write a script called hello.sh, make it executable, and run it. Then you take it one step further: a name from the command line with a default, a name typed at the keyboard, an exit code your script sets and you check, and the strict-mode line set -euo pipefail that catches mistakes early. Last, you install ShellCheck and use it to find the three errors beginners hit most.
Bash is everywhere servers are. It runs automation scripts, the run: steps of most CI jobs, Docker entrypoints, cron jobs and the day-to-day work of server administration. If you plan to work in DevOps, cloud or security, you will read and write it often.
You work in a throwaway folder, ~/hello-bash-demo, and delete it at the end.
Step 1 · Check which shell and Bash version you have
Ubuntu 26.04 includes Bash, so there is nothing to install yet. Check the version and the shell you log in with:
bash --version
getent passwd "$(id -un)" | cut -d: -f7bash --version prints a short banner with the version on its first line. The second command looks up your account in the user database (getent passwd), using your user name from id -un, and cut keeps the seventh colon-separated field, which is your login shell.
On Windows, install Ubuntu with WSL and follow the Ubuntu commands. On macOS, the default login shell has been zsh since macOS Catalina, and the /bin/bash Apple ships is version 3.2 from 2007. The scripts here are Bash scripts, not zsh scripts, so on a Mac install a current Bash with Homebrew:
brew install bashThe #!/usr/bin/env bash line you write in the next step then finds the Homebrew Bash first on your path. On an Android phone, the Termux app gives you a Bash shell too, which is a cheap way to practise without a laptop.
Check it worked
The first line of the banner starts GNU bash, version 5.3. The login shell prints as /bin/bash on a default Ubuntu account. On a Mac, echo "$SHELL" prints /bin/zsh, which is expected; Bash scripts still run in Bash through the shebang line.
Step 2 · Write hello.sh and run it
Make the demo folder and create the script:
mkdir -p ~/hello-bash-demo
cd ~/hello-bash-demo
cat > hello.sh <<'SH'
#!/usr/bin/env bash
# Print a greeting.
echo "Hello, world!"
SHThe cat > hello.sh <<'SH' line writes everything up to the closing SH into the file. You could type the same three lines in a text editor instead. Line by line:
#!/usr/bin/env bashis the shebang. When you run the file as a program, the kernel reads these first two characters,#!, and hands the file to the program named after them./usr/bin/env bashmeans "findbashon my path", which picks up a newer Bash on systems where/bin/bashis old.# Print a greeting.is a comment. Bash ignores everything from#to the end of a line.echo "Hello, world!"runs the built-inechocommand, which prints its arguments followed by a newline. The quotes make the text one argument.
A new file is not executable. Give it execute permission and run it:
chmod +x hello.sh
ls -l hello.sh
./hello.shchmod +x adds the execute permission bits. ./ means "in this folder". You need it because the current folder is not on your path, so a plain hello.sh gives command not found.
Check it worked
ls -l shows the permissions starting -rwx, where the x is new. ./hello.sh prints Hello, world!. If you see Permission denied, the chmod +x step did not run on this file.
Step 3 · See why bash hello.sh and sh hello.sh can differ
There are three ways to run a script, and they do not all use the shebang:
./hello.shuses the shebang, so Bash runs it.bash hello.shruns Bash directly, which reads the file. The shebang is only a comment then, and the file does not need execute permission.sh hello.shrunssh. On Ubuntu,/bin/shis dash, a smaller and faster shell that follows the POSIX standard and does not have Bash's extra features.
Write a script that uses a Bash-only feature, the [[ ... ]] test, and run it both ways:
cat > differ.sh <<'SH'
#!/usr/bin/env bash
name="Ada"
if [[ $name == A* ]]; then
echo "$name starts with A"
fi
SH
bash differ.sh
sh differ.sh[[ $name == A* ]] asks whether name matches the pattern "A followed by anything". Bash understands it. Dash does not have a [[ command, prints an error and skips the if body.
Check it worked
bash differ.sh prints Ada starts with A. sh differ.sh prints an error like differ.sh: 3: [[: not found and nothing else. The rule to keep: run Bash scripts with ./script.sh or bash script.sh, never sh script.sh.
Step 4 · Take a name from the command line
The words after a script's name arrive as numbered variables: $1 is the first, $2 the second. Write greet.sh, which greets the first word you give it, or World when you give none:
cat > greet.sh <<'SH'
#!/usr/bin/env bash
name="${1:-World}"
echo "Hello, ${name}!"
SH
chmod +x greet.sh
./greet.sh
./greet.sh Amina
./greet.sh "Mary Jane"name="${1:-World}"sets the variablename.${1:-World}means "the value of$1, orWorldif it is empty or not set". There are no spaces around=: Bash requires that, and the last step shows what happens otherwise."${name}"reads the variable. The braces mark where the name ends, so the!after it is plain text. The double quotes keep the value as one piece even when it contains spaces.
The quotes around "Mary Jane" on the command line matter too. Without them, the shell splits the words before your script sees them, $1 is Mary and $2 is Jane.
Check it worked
The three runs print Hello, World!, Hello, Amina! and Hello, Mary Jane!. Try ./greet.sh Mary Jane without quotes: it prints Hello, Mary!.
Step 5 · Read a name from the keyboard
read takes a line of input and stores it in a variable. Write ask.sh:
cat > ask.sh <<'SH'
#!/usr/bin/env bash
read -r -p "What is your name? " name
echo "Hello, ${name}. Nice to meet you."
SH
chmod +x ask.sh-p "What is your name? "prints a prompt before waiting.-rkeeps backslashes as typed. Without it,readtreats\` as an escape character and removes it, so a name typed as `A\Bcomes out asAB. Use-revery time unless you have a reason not to.nameat the end is the variable that receives the line.
Run it and type your name, then press Enter:
./ask.shScripts often get their input from another program instead of a person. A pipe, |, sends one command's output into the next command's input, and read takes it from there:
printf 'Chidinma\n' | ./ask.sh
printf 'Ọláolúwa\n' | ./ask.shBash passes text through as bytes, so names in Yoruba, Amharic or any other script come through unchanged as long as your terminal uses UTF-8, which Ubuntu does by default.
Check it worked
Typing Kofi at the prompt prints Hello, Kofi. Nice to meet you. The piped runs print Hello, Chidinma. Nice to meet you. and the same line for Ọláolúwa, with its accents intact. With piped input the prompt is not shown, because read only prints it when input comes from a terminal.
Step 6 · Check exit codes with $?
Every command ends with an exit code, a number from 0 to 255. Zero means success; anything else means failure, and the program chooses what each number means. Bash puts the last command's code in $?. grep -q searches a file quietly and reports only through its exit code:
grep -q root /etc/passwd; echo "exit code: $?"
grep -q no-such-user /etc/passwd || echo "exit code: $?"The ; runs the second command after the first whatever happens. The || runs the right-hand side only when the left-hand side fails, and inside it $? still holds the failed command's code. if, && and || all decide what to do from exit codes.
Your scripts can set their own. Add a check to greet.sh that refuses names containing digits:
cat > greet.sh <<'SH'
#!/usr/bin/env bash
name="${1:-World}"
if [[ $name == *[0-9]* ]]; then
echo "A name should not contain digits: ${name}" >&2
exit 2
fi
echo "Hello, ${name}!"
SH
./greet.sh Tunde; echo "exit code: $?"
./greet.sh R2D2 || echo "exit code: $?"*[0-9]*is a pattern: anything, then one digit, then anything.>&2sends the message to standard error, the stream meant for errors, so it does not mix with the script's normal output when someone pipes or saves it.exit 2stops the script and sets its exit code to 2. A script that reaches its last line exits with the code of the last command it ran, hereecho, which is 0.
Check it worked
The grep lines print exit code: 0 and exit code: 1. ./greet.sh Tunde prints Hello, Tunde! and exit code: 0. ./greet.sh R2D2 prints the error message and exit code: 2.
Step 7 · Turn on strict mode with set -euo pipefail
By default Bash carries on after a failed command, treats a misspelt variable as empty, and ignores failures in the middle of a pipe. Each of those can turn a small mistake into a script that deletes or overwrites the wrong thing. One line near the top changes all three:
set -estops the script when a command fails, unless the failure is checked byif,||or&&.set -umakes using an unset variable an error instead of an empty string.set -o pipefailmakes a pipe fail if any command in it fails, not only the last.
See -u catch a typo. This script misspells name as nmae:
cat > strict.sh <<'SH'
#!/usr/bin/env bash
set -euo pipefail
name="${1:-World}"
echo "Hello, ${nmae}!"
echo "This line never runs."
SH
chmod +x strict.sh
./strict.sh || echo "exit code: $?"Without set -u it would print Hello, ! and carry on. Note that ${1:-World} is safe under set -u, because the default covers the missing argument; a bare $1 with no argument would stop the script.
Add the line to greet.sh so it becomes the second line of the file:
cat > greet.sh <<'SH'
#!/usr/bin/env bash
set -euo pipefail
name="${1:-World}"
if [[ $name == *[0-9]* ]]; then
echo "A name should not contain digits: ${name}" >&2
exit 2
fi
echo "Hello, ${name}!"
SH
./greet.sh
./greet.sh ZaneleCheck it worked
./strict.sh prints an error naming line 4 and nmae: unbound variable, does not print This line never runs., and the || shows exit code: 1. The new greet.sh prints Hello, World! and Hello, Zanele!.
Step 8 · Catch beginner mistakes with ShellCheck
ShellCheck reads a script without running it and explains likely bugs, each with a code you can look up at shellcheck.net. Install it (a download of about 3 MB):
sudo apt update
sudo apt install -y shellcheck
shellcheck --versionNow write a script with the two most common mistakes:
cat > broken.sh <<'SH'
#!/usr/bin/env bash
greeting = "Hello"
file="my notes.txt"
touch $file
SH
shellcheck broken.sh || echo "ShellCheck found problems"greeting = "Hello"has spaces around=. Bash then readsgreetingas a command name with the arguments=and"Hello", and fails withgreeting: command not found. ShellCheck reports it as SC1068.touch $fileis unquoted. The shell splits the value at the space, sotouchcreates two files,myandnotes.txt. ShellCheck reports SC2086 and suggests"$file".
The third common mistake is invisible: Windows line endings. A file saved by some Windows editors ends each line with a carriage return and a newline, and Bash treats the carriage return as part of each command. Make such a file, see it fail, and fix it:
printf '#!/usr/bin/env bash\r\necho "Hello from Windows"\r\n' > crlf.sh
chmod +x crlf.sh
./crlf.sh || echo "exit code: $?"
tr -d '\r' < crlf.sh > fixed.sh
chmod +x fixed.sh
./fixed.shtr -d '\r' deletes every carriage return and writes the cleaned copy to fixed.sh. To avoid the problem, set your editor to save with LF line endings; VS Code shows CRLF or LF in its status bar. ShellCheck flags carriage returns as SC1017.
Finally, check your own scripts. ShellCheck prints nothing and exits with code 0 when it finds nothing to report:
shellcheck hello.sh greet.sh ask.sh
echo "ShellCheck exit code: $?"Check it worked
shellcheck --version shows version: 0.11.0. The broken.sh report points at line 2 about the spaces around = and, once that is fixed, at line 4 about quoting; it ends with ShellCheck found problems. ./crlf.sh fails with an error that mentions bash and a stray \r or No such file or directory, then fixed.sh prints Hello from Windows. The last check prints only ShellCheck exit code: 0.
When you are done, remove the demo folder. This deletes everything in ~/hello-bash-demo:
cd ~
rm -rf ~/hello-bash-demoNext steps
- Get faster at the prompt you just scripted: use the terminal as a programmer.
- Run a script on a timetable: schedule jobs with cron.
- Compare with the shell Windows administrators use: Hello World in PowerShell.
Related tutorials
- Hello World in Zig: zig run, zig build and zig ccYou will install a signature-checked Zig 0.17.1 on Ubuntu 26.04, write and run Hello World with the current I/O API, build a small project with zig build that handles errors explicitly, and use zig cc to compile a C program for Linux and for Windows.
- Hello World in x86-64 assembly with NASM and Linux system callsYou will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.
- Hello World in WebAssembly text format with wabt and WasmtimeYou will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials