Definition and Purpose
- Bash (Bourne Again Shell) is a command-line shell and scripting language for Unix-like operating systems, including Linux.
- It serves as an interface between the user and the operating system, allowing command execution, file manipulation, and automation via scripts.
- Default shell on most Linux distributions and macOS (until macOS Catalina switched to zsh).
Core Features
- Command Execution: Run commands like
ls
,cd
,mkdir
(via aliases or built-ins). - Scripting: Write scripts using variables, loops, conditionals, and functions.
- Pipes and Redirection: Use
|
to pipe output to another command,>
to redirect output to files,<
for input redirection. - Job Control: Manage background processes with
&
,fg
,bg
, andjobs
. - Tab Completion: Autocomplete commands, file names, and paths.
- History: Access previous commands with
history
or arrow keys.
Basic Syntax
- Commands:
command [options] [arguments]
(e.g.,ls -l /home
). - Variables: Declare with
name=value
(e.g.,MY_VAR=hello
), access with$name
. - Comments: Start with
#
. - Shebang:
#!/bin/bash
at the top of scripts to specify interpreter.
Essential Commands
- File Operations:
cp
(copy),mv
(move),rm
(remove),touch
(create empty file). - Directory Navigation:
cd
(change directory),pwd
(print working directory). - Text Processing:
cat
(display file),grep
(search),sed
(edit streams),awk
(pattern scanning). - System Info:
uname
,top
,df
(disk free),ps
(processes). - Permissions:
chmod
(change mode),chown
(change owner).
Scripting Basics
- Conditionals:
if [ condition ]; then ...; fi
(e.g.,[ "$VAR" -eq 5 ]
checks if VAR equals 5). - Loops:
for i in {1..5}; do echo $i; done
orwhile [ condition ]; do ...; done
. - Functions:
function_name() { commands; }
. - Exit Codes:
$?
returns the exit status of the last command (0 = success, non-zero = failure).
Configuration Files
- ~/.bashrc: User-specific settings (aliases, functions, prompt customization).
- ~/.bash_profile: Loaded on login shells (often sources
.bashrc
). - /etc/bash.bashrc: System-wide configuration.
Common Tools and Utilities
- find: Search for files (e.g.,
find / -name "file.txt"
). - xargs: Pass output as arguments to another command.
- curl/wget: Download files from the web.
- tar: Archive files (e.g.,
tar -cvf archive.tar dir/
).
Key Concepts
- Environment Variables:
PATH
(executable search path),HOME
(user’s home directory),PS1
(prompt string). - Globbing: Use
*
(any characters),?
(single character) for pattern matching (e.g.,ls *.txt
). - Quoting: Single quotes (
'
) preserve literal value, double quotes ("
) allow variable expansion. - Subshells: Run commands in a separate shell with
( )
or backticks`
.
Practical Examples
- List files and filter:
ls -l | grep "txt"
. - Simple script:
#!/bin/bash for i in {1..3}; do echo "Number: $i" done
- Check disk usage:
df -h | awk '{print $5}'
.
Strengths and Limitations
- Strengths: Lightweight, powerful for automation, widely supported.
- Limitations: Slower than compiled languages for heavy computation, less intuitive for complex data structures.
Resources
- Official Manual:
man bash
(on any Linux system) or GNU Bash Manual. - Learning: LinuxCommand.org, Bash Guide for Beginners.
- Video: Bash Scripting Tutorial by TechWorld with Nana.
- Cheat Sheet: Devhints Bash.
Adjacent Topics
- Other Shells: sh (Bourne Shell), zsh, fish, ksh.
- Linux Fundamentals: File system hierarchy, permissions, processes.
- Text Processing: Regular expressions, sed, awk.
- DevOps Tools: Cron (scheduling), SSH, Docker (bash often used in containers).
- Programming: Python, Perl (alternatives for scripting).
This covers the essentials of Bash for quick mastery on Linux systems. Practice with real commands and scripts is key to fluency.