scripting:bash
Table of Contents
Summary of Main Bash Scripting Concepts and Commands
Bash (Bourne Again Shell) is a powerful command-line interpreter commonly used in Linux and Unix environments. Below is a summary of essential Bash commands and scripting concepts.
Basic Commands
- echo - Outputs text to the terminal. Example: `echo “Hello, World!”`
- pwd - Prints the current working directory.
- ls - Lists the contents of a directory. Example: `ls -l`
- cd - Changes the working directory. Example: `cd /home/user`
- cp - Copies files or directories. Example: `cp source.txt destination.txt`
- mv - Moves or renames files or directories. Example: `mv old.txt new.txt`
- rm - Removes files or directories. Example: `rm file.txt`
- touch - Creates an empty file or updates a file's timestamp. Example: `touch newfile.txt`
Variables and Parameters
- Variable Definition: Assigns values to variables. Example: `name=“John”`
- Variable Usage: References variables using `$`. Example: `echo $name`
- User Input: Reads user input. Example:
```
read name echo "Hello, $name!" ```
Conditionals
- if-else Statements - Executes commands based on conditions:
```
if [ "$name" = "John" ]; then
echo "Hello, John!"
else
echo "Who are you?"
fi
```
* **[[bash:conditionals:case|case Statements]]** - Matches patterns to execute specific commands:
```
case $fruit in
apple) echo "This is an apple";;
banana) echo "This is a banana";;
*) echo "Unknown fruit";;
esac
```
Loops
- for Loop - Iterates over a list of items:
```
for i in 1 2 3; do
echo "Number: $i"
done
```
* **[[bash:loops:while|while Loop]]** - Repeats commands while a condition is true:
```
count=0
while [ $count -lt 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
```
Functions
- Defining Functions - Groups commands into reusable functions:
```
greet() {
echo "Hello, $1!"
}
greet "Alice"
```
* **[[bash:functions:return_values|Returning Values]]** - Functions can return status codes using `return`.
Input/Output Redirection
- Redirection - Redirects output to a file. Example: `echo “Text” > output.txt`
- Appending Output - Appends output to a file. Example: `echo “More text” » output.txt`
- Standard Input (stdin) - Redirects input from a file. Example: `cat < input.txt`
- Piping - Passes the output of one command as input to another. Example: `ls | grep “file”`
File Management
- File Testing - Tests file conditions. Example:
```
if [ -f "file.txt" ]; then
echo "File exists"
fi
```
* Common file tests:
* `-f` - Checks if a file exists and is regular.
* `-d` - Checks if a directory exists.
* `-r` - Checks if a file is readable.
* `-w` - Checks if a file is writable.
Process Management
- Background Processes - Runs a command in the background using `&`. Example: `sleep 10 &`
- jobs - Lists background jobs.
- kill - Terminates a process. Example: `kill 1234`
Error Handling
- Exit Status - The exit status of the last command can be accessed using `$?`.
- set -e - Stops script execution on any error.
Comments
- Single-line Comments - Starts with `#`. Example: `# This is a comment`
- Multi-line Comments - Use multiple `#` lines for multi-line comments.
This summary covers the fundamental concepts of Bash scripting. Each section can be expanded with detailed examples and explanations.
scripting/bash.txt · Last modified: 2025/02/12 07:59 by 85.219.17.206
