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
-oOPTION- Set an option like
set -o.
Nothing matches this filter.
Examples 10
$ run as a normal user# run as root (or with sudo)
1Run a script
bash backup.shWorks even without the x permission.
2Check the syntax
bash -n backup.shNo output = no syntax error.
3Trace a script
bash -x backup.sh 2>&1 | lessEach command with filled-in values (lines start with +).
4Run one command line
bash -c 'for i in 1 2 3; do echo $i; done'Useful with sudo, ssh and find -exec.
5A clean shell for tests
bash --noprofile --norcWithout your aliases and settings.
6Start a login shell
bash -lReads the profile files again (after changes).
7Which shell am I using?
$SHELL is the login shell; ps shows the running one.
8Version (for script features)
bash --version | head -1RHEL 10 has bash 5.2.
9Run a script with sudo and a heredoc
sudo bash -s <<'EOF2' dnf -y upgrade systemctl restart httpd EOF2Several root commands in one go.
10Find scripts with strict settings
grep -l 'set -euo pipefail' /usr/local/bin/*.shScripts that stop on errors.
Nothing matches this filter.
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).