Home > Environment & Utility > bash

bash: GNU Bourne-Again SHell

bash is a powerful command-line interpreter used as the default shell on most Linux systems. It provides features like script execution, command history, aliases, and job control, enabling users to interact with the system and automate tasks.

Overview

bash is an enhanced version of the Bourne Shell (sh), combining interactive shell features with powerful scripting capabilities. It offers convenient features such as customizable prompts, command completion, and history search, making it essential for writing complex automation scripts.

Key Features

Core functionalities provided by bash.

  • Command history and editing
  • Tab completion (for files, commands, variables, etc.)
  • User customization via aliases and functions
  • Job control (managing background/foreground processes)
  • Robust shell scripting support
  • Environment and shell variable management

Key Options

Major options that can be used when running the bash command itself. These control how scripts are executed or how the shell behaves.

Execution Modes

Script Debugging

Generated command:

Try combining the commands.

Description:

`bash` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Examples of running the bash shell in various ways or processing scripts.

Start a basic interactive shell

bash

Starts a new bash shell session.

Execute a command string

bash -c "echo 'Current directory: $(pwd)'"

Uses bash to execute a specific command string and then exit.

Execute a script file

bash my_script.sh

Runs a specified bash script file.

Start a login shell

bash --login

Starts a bash shell that loads initialization files as if it were a login shell.

Execute script in debugging mode

bash -x my_script.sh

Traces and prints each command as the script executes.

Execute script from standard input

echo "ls -l" | bash -s

Allows the bash shell to execute commands passed via a pipe.

Tips & Considerations

Tips for making bash usage more efficient and points to consider when writing scripts.

Key Configuration Files

Major configuration files used to customize the behavior of the bash shell.

  • ~/.bashrc: Executed when an interactive shell starts. Used for defining aliases, functions, prompt settings, etc.
  • ~/.bash_profile: Executed when a login shell starts. Used for setting environment variables, calling other initialization scripts, etc.
  • /etc/profile: System-wide login shell configuration file. Contains settings applied to all users' login shells.

Scripting Best Practices

Recommendations for writing robust and efficient bash scripts.

  • Start scripts with `#!/bin/bash` (shebang) to explicitly specify bash as the interpreter.
  • Always quote variables to prevent issues with spaces or special characters (e.g., `echo "$VAR"`).
  • Use `set -e` to exit immediately if a command exits with a non-zero status, and `set -u` to treat unset variables as an error.
  • Use functions to modularize code and improve readability.
  • Add comments to explain the script's purpose and complex logic.

Same category commands