Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

bash

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

Niveau
RHCSA-examen
Groep
Shell en hulp
Root nodig
Nee
Pakket
bash

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

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

Opties 20

Meest gebruikt

-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.

Voorbeelden 10

$ als gewone gebruiker# als root (of met 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.

Bestanden

/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.

Exitcodes

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).