Complete Bash Tutorial with Usage Examples

Table of Contents

1. What is Bash?

Bash (Bourne Again SHell) is a Unix shell and command language, developed for the GNU Project as a free software replacement for the Bourne shell (sh). It's the default command-line interpreter on most Linux distributions and macOS (older versions), providing a powerful interface for interacting with the operating system.

Bash allows users to execute commands, navigate the file system, manage files and processes, and automate tasks through shell scripting.

Key Characteristics:

Tip for Practice: The best way to learn Bash is by doing. Open a terminal on your Linux machine (or use WSL on Windows, or Terminal on macOS) and type along with the examples.

2. Accessing Bash

You access Bash through a terminal emulator.

# Check your current shell
echo $SHELL
# Output will likely be /bin/bash or /bin/zsh (zsh is default on newer macOS)

# If not bash, you can usually start bash by typing:
bash

3. Basic Commands & Navigation

Fundamental commands for getting around and interacting with your system.

4. Working with Files and Directories

Creating, moving, copying, and deleting files and folders.

5. File Permissions & Ownership

Linux is a multi-user system, and file permissions control who can read, write, or execute files and directories.

Viewing Permissions (`ls -l`):

ls -l my_script.sh
# Example Output: -rwxr-xr-- 1 user group 1234 Jul 19 10:00 my_script.sh

Changing Permissions (`chmod`):

Uses symbolic mode (r, w, x, u, g, o, a) or octal (numeric) mode.

Changing Ownership (`chown`, `chgrp`):

Requires `sudo` (superuser privileges).

6. Input/Output Redirection

Control where command output goes and where input comes from.

7. Pipes & Filters

The pipe operator (`|`) sends the standard output of one command as the standard input to another command. Filters are commands that process data (like `grep`, `awk`, `sed`).

8. Variables

Store values in memory for later use. Variables are case-sensitive.

9. Arithmetic Operations

Bash can perform basic arithmetic using `(( ))` or `expr`.

10. Command Substitution

Allows the output of a command to be used as an argument to another command or assigned to a variable.

11. Quoting

Used to control how Bash interprets special characters.

12. Shell Scripting Fundamentals

Automate tasks by writing a series of commands in a file.

A. Creating a Script:

  1. Create a new file (e.g., `my_script.sh`) using a text editor (Nano, Vim, VS Code).
  2. Add the shebang line at the very beginning: `#!/bin/bash`.
  3. Write your commands line by line.
  4. Save the file.
# my_script.sh
#!/bin/bash
# This is my first Bash script.

echo "--- Script Start ---"
echo "Current directory: $(pwd)"
echo "Listing files:"
ls -lh
echo "--- Script End ---"

B. Making Executable:

You need to give the script execute permissions.

chmod +x my_script.sh

C. Running the Script:

./my_script.sh

Or, if it's in your PATH:

my_script.sh

13. Conditional Statements (if/else)

Execute code blocks based on conditions. The most common form uses `if`, `elif` (else if), and `else` with `[[ ]]` for evaluations.

14. Looping Constructs (for, while)

Repeat a block of code multiple times.

15. Functions

Group commands into reusable blocks of code.

16. Command-Line Arguments

Scripts can take inputs directly from the command line.

17. Error Handling & Debugging

Making scripts robust and finding issues.

18. Job Control

Manage commands running in the foreground or background.

19. Regular Expressions (Regex) with Bash Tools

Regex is a powerful language for pattern matching in text. Bash itself has limited direct regex support, but many common CLI tools leverage it.

20. Alias & History

Improve productivity by creating shortcuts and reusing commands.

21. Advanced Topics & Best Practices

Beyond the basics, Bash offers more powerful features and best practices for writing robust scripts.

The Power of the Command Line Awaits!

Bash is an incredibly powerful and versatile tool. While the initial learning curve might seem steep, mastering Bash commands and scripting will dramatically improve your efficiency in managing Linux/Unix systems, automating tasks, and performing complex operations. Embrace the command line, practice regularly, and unlock a new level of control over your computing environment.