LinuxCommandLibrary

bash

Start a new Bash shell

TLDR

Start an interactive shell session

$ bash
copy

Start an interactive shell session without loading startup configs
$ bash --norc
copy

Execute specific [c]ommands
$ bash -c "[echo 'bash is executed']"
copy

Execute a specific script
$ bash [path/to/script.sh]
copy

E[x]ecute a specific script, printing each command before executing it
$ bash -x [path/to/script.sh]
copy

Execute a specific script and stop at the first [e]rror
$ bash -e [path/to/script.sh]
copy

Execute specific commands from stdin
$ [echo "echo 'bash is executed'"] | bash
copy

Start a [r]estricted shell session
$ bash -r
copy

SYNOPSIS

bash [options] [file]
bash [options] -c command-string [command-name [arguments...]]
bash [options] -s [arguments...]

PARAMETERS

-c
    Execute command_string as a command.

-i
    Force the shell to be interactive.

-l, --login
    Make this shell a login shell.

--norc
    Do not read the startup file ~/.bashrc.

--rcfile
    Execute commands from file instead of ~/.bashrc.

-s
    Read commands from standard input.

-x
    Print commands and their arguments as they are executed.

--version
    Display version information and exit.

DESCRIPTION

bash, or the Bourne-Again SHell, is a Unix shell and command language interpreter. It's the default interactive shell on most Linux distributions and macOS. Bash executes commands read from standard input, a file, or a string. It supports powerful features such as command-line editing, command history, job control, aliases, functions, and programmable completion. Beyond interactive use, bash is a robust scripting language, widely used for automating tasks, system administration, and general-purpose programming. It combines features from the original Bourne shell (sh) with enhancements from csh and ksh, making it a versatile and widely adopted tool for both novice and experienced users to interact with the operating system.

CAVEATS

bash behavior can differ significantly between login and non-login shells, and interactive vs. non-interactive sessions, due to different startup file (.profile, .bash_profile, .bashrc) processing. Scripts intended for portability should ideally adhere to POSIX sh syntax to avoid bash-specific features. Historically, bash was notably affected by the "Shellshock" vulnerability, highlighting the importance of keeping software updated.

STARTUP FILES

When bash starts, it executes commands from various startup files depending on whether it's a login shell, non-login interactive shell, or non-interactive shell. Common files include ~/.bash_profile (login shells), ~/.bashrc (interactive non-login shells), and /etc/profile and /etc/bash.bashrc (system-wide). These files are crucial for setting environment variables, aliases, and functions.

JOB CONTROL

bash supports job control, allowing users to manage multiple processes running in the foreground or background. Commands can be suspended (Ctrl+Z), sent to the background (& or bg), brought to the foreground (fg), and listed (jobs). This feature enhances productivity for interactive shell sessions.

HISTORY

bash was created by Brian Fox for the GNU Project as a free software replacement for the Bourne shell (sh). First released in 1989, it quickly gained popularity due to its robust feature set, incorporating desirable elements from ksh and csh, such as command-line editing and history. It became the default shell for most Linux distributions and macOS, playing a pivotal role in the open-source ecosystem.

SEE ALSO

sh(1), ksh(1), zsh(1), dash(1), csh(1), expect(1), man(1)

Copied to clipboard