Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

bash

The Bourne Again Shell: the default command shell of RHEL, and the program that runs shell scripts.

Level
RHCSA exam
Group
Shell and help
Needs root
No
Package
bash

What it does

bash reads commands from the terminal or from a script. A login shell reads /etc/profile, /etc/profile.d/*.sh and ~/.bash_profile; an interactive shell reads ~/.bashrc (and /etc/bashrc). bash -n script checks the syntax; bash -x script shows each command as it runs.

How to write it

bash [OPTION]... [-c COMMAND | SCRIPT [ARGUMENT]...]

Options 20

Most used

-c STRING
Run the commands in STRING.
-n
Read but do not run (syntax check).
-x
Print each command before running it (trace).
-v
Print each line as it is read.
-e
Exit at the first failing command.
-u
Error on unset variables.
-i
Interactive shell.
-l, --login
Act as a login shell (read the profile files).
-s
Read commands from standard input.
-r, --restricted
Restricted shell (no cd, no PATH changes …).
-D, --dump-strings
List translatable strings.
-O OPTION, +O OPTION
Set or unset a shopt option.
-o OPTION
Set an option like set -o.
--noprofile
Do not read the profile files.
--norc
Do not read ~/.bashrc.
--rcfile FILE
Read FILE instead of ~/.bashrc.
--posix
POSIX mode.
--debugger
Debugger profile.
--help
Show help.
--version
Show the version.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Run a script

    bash backup.sh

    Works even without the x permission.

  2. 2Check the syntax

    bash -n backup.sh

    No output = no syntax error.

  3. 3Trace a script

    bash -x backup.sh 2>&1 | less

    Each command with filled-in values (lines start with +).

  4. 4Run one command line

    bash -c 'for i in 1 2 3; do echo $i; done'

    Useful with sudo, ssh and find -exec.

  5. 5A clean shell for tests

    bash --noprofile --norc

    Without your aliases and settings.

  6. 6Start a login shell

    bash -l

    Reads the profile files again (after changes).

  7. 7Which shell am I using?

    echo $SHELL; ps -p $$ -o comm=
    /bin/bash
    bash

    $SHELL is the login shell; ps shows the running one.

  8. 8Version (for script features)

    bash --version | head -1

    RHEL 10 has bash 5.2.

  9. 9Run a script with sudo and a heredoc

    sudo bash -s <<'EOF2'
    dnf -y upgrade
    systemctl restart httpd
    EOF2

    Several root commands in one go.

  10. 10Find scripts with strict settings

    grep -l 'set -euo pipefail' /usr/local/bin/*.sh

    Scripts that stop on errors.

Files

/etc/profile, /etc/profile.d/
Login settings for all users.
/etc/bashrc
Interactive settings for all users.
~/.bash_profile, ~/.bashrc
Personal settings.
~/.bash_history
Command history.

Exit codes

0 :: The script or command worked. 1-255 :: The exit status of the last command (2 = wrong use of a builtin, 126 = cannot run, 127 = not found).