====== 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 ===== * **[[bash:commands:echo|echo]]** - Outputs text to the terminal. Example: `echo "Hello, World!"` * **[[bash:commands:pwd|pwd]]** - Prints the current working directory. * **[[bash:commands:ls|ls]]** - Lists the contents of a directory. Example: `ls -l` * **[[bash:commands:cd|cd]]** - Changes the working directory. Example: `cd /home/user` * **[[bash:commands:cp|cp]]** - Copies files or directories. Example: `cp source.txt destination.txt` * **[[bash:commands:mv|mv]]** - Moves or renames files or directories. Example: `mv old.txt new.txt` * **[[bash:commands:rm|rm]]** - Removes files or directories. Example: `rm file.txt` * **[[bash:commands:touch|touch]]** - Creates an empty file or updates a file's timestamp. Example: `touch newfile.txt` ===== Variables and Parameters ===== * **[[bash:variables:definition|Variable Definition:]]** Assigns values to variables. Example: `name="John"` * **[[bash:variables:usage|Variable Usage:]]** References variables using `$`. Example: `echo $name` * **[[bash:variables:read|User Input:]]** Reads user input. Example: ``` read name echo "Hello, $name!" ``` ===== Conditionals ===== * **[[bash:conditionals:if_else|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 ===== * **[[bash:loops:for|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 ===== * **[[bash:functions:definition|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 ===== * **[[bash:io:redirection|Redirection]]** - Redirects output to a file. Example: `echo "Text" > output.txt` * **[[bash:io:append|Appending Output]]** - Appends output to a file. Example: `echo "More text" >> output.txt` * **[[bash:io:stdin|Standard Input (stdin)]]** - Redirects input from a file. Example: `cat < input.txt` * **[[bash:io:pipe|Piping]]** - Passes the output of one command as input to another. Example: `ls | grep "file"` ===== File Management ===== * **[[bash:files:test|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 ===== * **[[bash:processes:background|Background Processes]]** - Runs a command in the background using `&`. Example: `sleep 10 &` * **[[bash:processes:jobs|jobs]]** - Lists background jobs. * **[[bash:processes:kill|kill]]** - Terminates a process. Example: `kill 1234` ===== Error Handling ===== * **[[bash:error_handling:exit_status|Exit Status]]** - The exit status of the last command can be accessed using `$?`. * **[[bash:error_handling:set|set -e]]** - Stops script execution on any error. ===== Comments ===== * **[[bash:comments:single_line|Single-line Comments]]** - Starts with `#`. Example: `# This is a comment` * **[[bash:comments:multi_line|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.