bash

shellslinux
The bash command is one of the most frequently used commands in Linux/Unix-like operating systems. bash The bash (Bourne-Again SHell) is a command processor that typically runs in a text window where the user types commands that cause actions. Bash can also read and execute commands from a file, called a shell script.

Quick Reference

Command Name:

bash

Category:

shells

Platform:

linux

Basic Usage:

bash [options] [arguments]

Common Use Cases

    Syntax

    bash [options] [file [arguments]]
    bash [options] -c command_string [arguments]

    Options

    Option Description
    -c string Execute commands from string
    -i Force shell to be interactive
    -l Make bash act as if invoked as a login shell
    -r Start a restricted shell
    -s Read commands from standard input
    -v Print shell input lines as they are read
    -x Print commands and their arguments as they are executed
    --norc Do not read startup files (the .bashrc file)
    --noprofile Do not read the startup files /etc/profile or ~/.profile
    --rcfile file Execute commands from file instead of ~/.bashrc
    --posix Change the behavior of bash to follow the POSIX standard
    --version Display version information and exit
    --help Display a usage message and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the bash command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    # Start a new interactive bash shell
    bash
    # Run a bash script bash myscript.sh
    # Execute a command in bash bash -c "echo Hello World"
    # Pass arguments to a script bash myscript.sh arg1 arg2
    # Start bash with a login shell bash -l

    Advanced Examples:

    # Execute bash with debugging output
    bash -x myscript.sh
    # Run a script in POSIX compatibility mode bash --posix myscript.sh # Check syntax of a script without executing it bash -n myscript.sh # Execute with restricted permissions bash -r # Specify a startup file to use instead of .bashrc bash --rcfile /path/to/custom_bashrc # Set a custom prompt bash --init-file <(echo 'PS1="Custom> "') # Run commands from standard input echo "echo Hello World" | bash # Run a script with custom environment variables CUSTOM_VAR="value" bash myscript.sh

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Bash Features:

    Bash is one of the most popular shells in Linux and Unix environments. It offers several powerful features:

    • Command-line editing: Use arrow keys, Ctrl+A (beginning of line), Ctrl+E (end of line), etc.
    • Command history: Access previous commands with the up/down arrows or Ctrl+R for search.
    • Tab completion: Auto-complete filenames, commands, and more by pressing Tab.
    • Job control: Run processes in the background with &, use fg and bg commands to manage jobs.
    • Aliases: Create shortcuts for frequently used commands (e.g., alias ll='ls -la').
    • Shell scripting: Use variables, conditionals, loops, and functions for automation.
    • Command substitution: Embed command output in other commands using $(command) or backticks.
    • Redirection: Redirect input/output with <, >, >>, 2>, etc.
    • Pipelines: Connect commands with pipes (|) to create powerful command chains.

    Configuration Files:

    • /etc/profile: System-wide configuration for login shells.
    • ~/.bash_profile, ~/.bash_login, ~/.profile: Personal configuration for login shells (read in this order).
    • ~/.bashrc: Personal configuration for interactive non-login shells.
    • ~/.bash_history: Stores command history.
    • ~/.bash_logout: Executed when a login shell exits.
    • ~/.inputrc: Configures readline behavior (command-line editing).

    Bash vs. Other Shells:

    • Bash is a superset of the Bourne shell (sh) with additional features.
    • It incorporates useful features from the Korn shell (ksh) and C shell (csh).
    • Alternatives include zsh (more features, better customization), fish (user-friendly, great defaults), and dash (faster, more POSIX-compliant).
    • Bash is the default shell in most Linux distributions and macOS (until Catalina).

    Security Considerations:

    • The -r (restricted) option limits what users can do, providing a more secure environment.
    • Always validate user input in bash scripts to prevent injection attacks.
    • Be cautious when using eval, as it can execute arbitrary code.
    • Use "set -e" in scripts to exit on errors rather than continuing with potentially harmful operations.
    • Consider using "set -u" to catch undefined variables rather than silently continuing.

    Related Commands

    These commands are frequently used alongside bash or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the bash command works in different scenarios.

    $ bash
    View All Commands