Kennisbank › Bash Shell › Frequently asked problems
bash shell starter 31 August 2026

Frequently asked problems

Bash is the standard shell on virtually every Linux system and scripting it is a core skill for anyone who manages servers. This article explains

Bash is the default shell on almost every Linux system, and scripting it is a core skill for anyone who manages servers. This article explains what Bash is, how to use history and expansion, and how to write a first working shell script. Aimed at system administrators and developers who work with Linux.

Prerequisites

  • A Linux system with Bash version 4.0 or higher (check with `bash --version`)
  • Access to a terminal and a text editor such as `nano`, `vim`, or `gedit`
  • Basic knowledge of Linux commands such as `ls`, `cd`, and `cat`

Steps

1. Check the Bash version

Run the following command to see which version of Bash your system uses:

bash --version

The output shows the version, for example `GNU bash, version 5.2.21(1)-release`. Work with at least version 4.0, because older versions lack some functionality such as associative arrays.

2. View the command history

Bash stores previously entered commands in the hidden file `.bash_history` in your home directory. Display the list with:

history

This returns numbered lines, for example `123 ls -la`. The history is enabled by default and records commands per session.

3. Use history expansion

With exclamation marks you can re-run previous commands without typing them again. The three most commonly used forms:

!!          # repeats the last command
!123        # runs the command on line 123
!grep       # runs the last command that started with 'grep'

This expansion only works in interactive shells, not in scripts. It saves time with long or complex commands you use frequently.

4. Create a first shell script

A shell script is a text file with Bash commands. Create a new file:

nano hallo.sh

Paste the following content into the file:

#!/bin/bash
echo "Hello from Pixzo"
mkdir -p /tmp/pixzo-test
echo "Directory /tmp/pixzo-test has been created"

The first line (shebang) tells the system that Bash should execute the script. The remaining lines run commands. Save the file with `Ctrl+O` and close it with `Ctrl+X`.

5. Make the script executable

Without execute permissions, the script will not start directly. Grant those permissions with:

chmod +x hallo.sh

This adds the execute bit to the file. Check the permissions with `ls -l hallo.sh` — the output shows `-rwxr-xr-x`.

6. Run the script

Start the script in two ways. Directly:

./hallo.sh

Or by explicitly calling Bash:

bash hallo.sh

Both methods give the same output. The difference: with `./hallo.sh` the system uses the interpreter from the shebang line; with `bash hallo.sh` it ignores that line and uses the Bash you call.

7. Combine commands with pipes and redirection

Bash lets you link commands. A pipe (`|`) sends the output of one command to the next. Redirection (`>`, `<`) sends output to a file or reads input from a file. A practical example:

ps aux | grep httpd > /tmp/httpd-processen.txt

This command shows all processes (`ps aux`), filters for lines containing `httpd` (`grep httpd`), and writes the result to a file. View the result with:

cat /tmp/httpd-processen.txt

8. Add logic with variables and conditions

A script only becomes useful with variables and conditional logic. Extend `hallo.sh`:

#!/bin/bash
DIR="/tmp/pixzo-test"
if [ -d "$DIR" ]; then
  echo "$DIR already exists"
else
  mkdir -p "$DIR"
  echo "$DIR created"
fi

The variable `DIR` stores the path. The `if` statement checks whether the directory exists and handles both cases. Save, make it executable with `chmod +x hallo.sh`, and run it:

./hallo.sh

Verification

Check that everything works:

./hallo.sh && ls -la /tmp/pixzo-test

If the script runs correctly, you will see the message that the directory exists or has been created, and `ls -la` shows the contents of the directory. Check the script's exit code with:

echo $?

A `0` means the script completed without errors.

Common problems

The script does not start with `./hallo.sh` and gives `Permission denied` The execute bit is missing. Run `chmod +x hallo.sh`.

`!123` does not work in the terminal History expansion is disabled or the line does not exist. Check with `history` which numbers are available. Enable expansion with `set -H`.

The script gives `bad interpreter: /bin/bash^M` The file contains Windows line endings (`\r\n`). Convert it with `sed -i 's/\r$//' hallo.sh` or save the file with Unix line endings in your editor.

Bash remains the standard in Linux distributions, meets POSIX specifications, and forms the basis for automation in init scripts, CI/CD pipelines, and system administration. The syntax is sometimes strict, but once you master the combination of pipes, variables, and conditions, you can build workflows that no graphical tool can match — and the question is not whether you need Bash, but when you will use it.

← Back to knowledge base
← Terug naar kennisbank