Here’s a summary of the most important concepts in Bash, along with examples:
### 1. **Basic Commands**
- **`ls`**: List directory contents.
```bash
ls -l
```
- **`cd`**: Change directory.
```bash
cd /path/to/directory
```
- **`pwd`**: Print working directory.
```bash
pwd
```
- **`echo`**: Display text or variable values.
```bash
echo "Hello, World!"
```
### 2. **Variable Assignment**
- **Assigning a value to a variable**:
```bash
name="John"
```
- **Accessing a variable**:
```bash
echo $name
```
### 3. **Conditionals (if statements)**
- **Basic if-else statement**:
```bash
if [ $age -ge 18 ]; then
echo "Adult"
else
echo "Minor"
fi
```
### 4. **Loops**
- **For loop**:
```bash
for i in {1..5}; do
echo "Number: $i"
done
```
- **While loop**:
```bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
```
### 5. **Functions**
- **Defining and calling a function**:
```bash
greet() {
echo "Hello, $1!"
}
greet "Alice"
```
### 6. **Command Substitution**
- **Using command output as a variable**:
```bash
current_date=$(date)
echo "Current date: $current_date"
```
### 7. **Redirection**
- **Redirecting output to a file**:
```bash
echo "This is a log" > log.txt
```
- **Appending output to a file**:
```bash
echo "New log entry" >> log.txt
```
- **Redirecting error output**:
```bash
command 2> error.log
```
### 8. **Piping**
- **Chaining commands**:
```bash
cat file.txt | grep "search_term"
```
### 9. **File Permissions**
- **Changing file permissions**:
```bash
chmod 755 script.sh
```
- **Changing file ownership**:
```bash
sudo chown user:group file.txt
```
### 10. **Process Management**
- **Running a process in the background**:
```bash
./script.sh &
```
- **Killing a process**:
```bash
kill <pid>
```
### 11. **Environment Variables**
- **Setting an environment variable**:
```bash
export PATH=$PATH:/new/path
```
- **Viewing environment variables**:
```bash
printenv
```
### 12. **File Manipulation**
- **Copying files**:
```bash
cp source.txt destination.txt
```
- **Moving/renaming files**:
```bash
mv oldname.txt newname.txt
```
- **Deleting files**:
```bash
rm file.txt
```
### 13. **String Manipulation**
- **Extracting a substring**:
```bash
str="Hello World"
echo ${str:0:5} # Output: Hello
```
### 14. **Exit Codes**
- **Checking exit status of the last command**:
```bash
if [ $? -eq 0 ]; then
echo "Success"
else
echo "Failure"
fi
```
### 15. **Arrays**
- **Declaring an array**:
```bash
arr=("apple" "banana" "cherry")
```
- **Accessing array elements**:
```bash
echo ${arr[0]} # Output: apple
```
### 16. **Input/Output**
- **Reading user input**:
```bash
read -p "Enter your name: " name
echo "Hello, $name"
```
### 17. **Scripting**
- **Writing a simple script**:
```bash
#!/bin/bash
echo "This is a script"
```
- **Making a script executable**:
```bash
chmod +x script.sh
```
### 18. **Job Control**
- **Listing jobs**:
```bash
jobs
```
- **Bringing a background job to the foreground**:
```bash
fg %1
```
### 19. **Wildcards**
- **Using wildcards for pattern matching**:
```bash
ls *.txt
```
### 20. **Comments**
- **Single-line comment**:
```bash
# This is a comment
```
These are key concepts that will help you work effectively with Bash, both interactively and in writing scripts.