Files
the_information_nexus/tech_docs/linux/bash.md
2024-05-01 12:28:44 -06:00

223 lines
6.1 KiB
Markdown

### 1. Bash Startup Files
- **`~/.bash_profile`, `~/.bash_login`, and `~/.profile`**: Used for login shells.
- **`~/.bashrc`**: Used for non-login shells. Essential for setting environment variables, aliases, and functions that are used across sessions.
### 2. Shell Scripting
- **Variables and Quoting**: Discusses how to correctly use and quote variables to avoid common pitfalls.
- **Conditional Execution**: Covers the use of `if`, `else`, `elif`, `case` statements, and the `[[ ]]` construct for test operations.
- **Loops**: Explains `for`, `while`, and `until` loops, with examples on how to iterate over lists, files, and command outputs.
- **Functions**: How to define and use functions in scripts for reusable code.
- **Script Debugging**: Using `set -x`, `set -e`, and other options to debug shell scripts.
### 3. Advanced Command Line Tricks
- **Brace Expansion**: Using `{}` for generating arbitrary strings.
- **Command Substitution**: Using `$(command)` or `` `command` `` to capture the output of a command.
- **Process Substitution**: Utilizes `<()` and `>()` for treating the output or input of a command as a file.
- **Redirection and Pipes**: Advanced uses of `>`, `>>`, `<`, `|`, and `tee` for controlling input and output streams.
### 4. Job Control
- **Foreground and Background Jobs**: Using `fg`, `bg`, and `&` to manage jobs.
- **Job Suspension**: Utilizing `Ctrl+Z` to suspend jobs and `jobs` to list them.
### 5. Text Processing Tools
- **`grep`, `awk`, `sed`**: Mastery of these tools for text processing and data extraction.
- **Regular Expressions**: Advanced patterns and their applications in text processing commands.
### 6. Networking Commands
- **`ssh`, `scp`, `curl`, and `wget`**: For remote access, file transfer, and downloading content from the internet.
- **`netstat`, `ping`, `traceroute`**: Basic networking diagnostics tools.
### 7. System Administration
- **File Permissions and Ownership**: Advanced manipulation with `chmod`, `chown`, and `chgrp`.
- **Process Management**: Using `ps`, `top`, `htop`, `kill`, `pkill`, and `killall` for process monitoring and management.
- **Disk Usage**: Utilizing `df`, `du`, and `lsblk` to monitor disk space and file system usage.
### 8. Environment Customization
- **Aliases and Functions**: Creating efficient shortcuts and reusable commands.
- **Prompt Customization**: Modifying the Bash prompt (`PS1`) for better usability and information display.
### 9. Package Management
- **For Linux**: Using package managers like `apt`, `yum`, or `dnf`.
- **For macOS**: Utilizing `brew` (Homebrew) for package management.
### 10. Security
- **File Encryption**: Using tools like `gpg` for encrypting and decrypting files.
- **SSH Keys**: Generating and managing SSH keys for secure remote access.
### Conclusion and Resources
Conclude with the importance of continuous learning and experimentation in mastering Bash. Provide resources for further exploration, such as the GNU Bash manual, advanced scripting guides, and forums like Stack Overflow.
This structure should provide a comprehensive guide for advanced CLI users to deepen their mastery of Bash on Linux and macOS systems. Each section can be expanded with examples, best practices, and detailed explanations tailored to advanced users' needs.
---
To create a practical and instructional guide for power users of the CLI, let's provide sample shell scripts and commands that embody the key areas of focus. These examples will help to solidify understanding and demonstrate the utility of Bash in various common scenarios.
### 1. Bash Startup Files
```bash
# ~/.bash_profile example
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
export PATH="$PATH:/opt/bin"
alias ll='ls -lah'
```
### 2. Shell Scripting
- **Variables and Quoting**:
```bash
greeting="Hello, World"
echo "$greeting" # Correctly quotes the variable.
```
- **Conditional Execution**:
```bash
if [[ -f "$file" ]]; then
echo "$file exists."
elif [[ -d "$directory" ]]; then
echo "$directory is a directory."
else
echo "Nothing found."
fi
```
- **Loops**:
```bash
# Iterate over files
for file in *.txt; do
echo "Processing $file"
done
# While loop
counter=0
while [[ "$counter" -lt 10 ]]; do
echo "Counter: $counter"
((counter++))
done
```
- **Functions**:
```bash
greet() {
echo "Hello, $1"
}
greet "World"
```
- **Script Debugging**:
```bash
set -ex # Exit on error and print commands and their arguments as they are executed.
```
### 3. Advanced Command Line Tricks
- **Brace Expansion**:
```bash
cp /path/to/source/{file1,file2,file3} /path/to/destination/
```
- **Command Substitution**:
```bash
current_dir=$(pwd)
echo "You are in $current_dir"
```
- **Process Substitution**:
```bash
diff <(ls dir1) <(ls dir2)
```
- **Redirection and Pipes**:
```bash
grep 'error' logfile.txt | tee errorlog.txt
```
### 4. Job Control
```bash
# Run a command in the background
long_running_process &
# Bring the last job to the foreground
fg
# Suspend the current job
Ctrl+Z
# List jobs
jobs
```
### 5. Text Processing Tools
- Using `awk` to sum the first column of a file:
```bash
awk '{ sum += $1 } END { print sum }' numbers.txt
```
### 6. Networking Commands
- Secure file transfer:
```bash
scp localfile.txt user@remotehost:/path/to/destination/
```
### 7. System Administration
- Monitoring disk usage:
```bash
df -h # Human-readable disk space of file systems
du -sh /path/to/directory # Disk usage of the specified directory
```
### 8. Environment Customization
- Customizing the Bash prompt:
```bash
export PS1='\u@\h:\w\$ '
```
### 9. Package Management
- Installing a package on Linux (Debian/Ubuntu):
```bash
sudo apt-get update && sudo apt-get install packagename
```
### 10. Security
- Generating an SSH key pair:
```bash
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
```
Each of these sections and examples can be further detailed and expanded upon in a comprehensive guide. The intention is to provide a solid foundation of practical Bash usage and scripting techniques, encouraging further exploration and mastery of the shell environment. Continuous learning and experimentation are key to becoming proficient in Bash scripting and command-line usage.