Bash Cheatsheet

What is Bash?

1. Introduction to Bash

Bash is a powerful command-line interpreter that serves as the default shell for most Linux distributions and macOS. It's also available on Windows 10.

Here's a breakdown of what Bash is and what it does:

What it is:

  • Shell: At its core, Bash is a shell, a program that provides a command-line interface (CLI) for interacting with the operating system. This means you type commands into a terminal window, and Bash executes them.
  • Interpreter: Bash interprets the commands you type, translating them into actions that the operating system can understand.
  • Scripting Language: Bash is also a scripting language, allowing you to write scripts (sequences of commands) to automate tasks.

What it does:

  • Execute Commands: You can use Bash to run built-in commands (like ls to list files) and external programs (like text editors or web browsers).
  • Manage Files and Directories: Create, delete, move, copy, and rename files and directories.
  • Control System Processes: Start, stop, and manage running processes.
  • Automate Tasks: Create scripts to perform repetitive tasks, such as system maintenance, file backups, or web scraping.
  • Customize Your Environment: Configure your shell environment with variables, aliases, and functions.

Key Features:

  • Command History: Bash remembers the commands you've previously entered, allowing you to easily recall and re-execute them.
  • Command Line Editing: Edit commands before executing them using arrow keys, backspace, and other editing keys.
  • Job Control: Manage background processes and bring them back to the foreground.
  • Scripting Capabilities: Write scripts to automate tasks and perform complex operations.
  • Extensive Built-in Commands: Offers a wide range of built-in commands for various tasks.

In essence, Bash is a versatile tool that provides a powerful and flexible way to interact with your computer's operating system. It's widely used by system administrators, developers, and power users who need to perform complex tasks or automate repetitive operations.

  • History and significance of Bash in Unix/Linux.

History of Bash

Bash, an acronym for "Bourne-Again SHell," was created by Brian Fox in 1987 as a free software replacement for the original Unix shell, the Bourne shell (often referred to as "sh"). It was developed as part of the GNU Project and was designed to be a versatile shell that could be used in a variety of Unix environments.

Significance of Bash in Unix/Linux

Bash's significance in the Unix/Linux world stems from several key factors:

  1. Compatibility: Bash was designed to be POSIX-compliant, meaning it adheres to the IEEE POSIX standard for shell and utilities. This ensures compatibility with a wide range of Unix-like systems and allows for portability of scripts written in Bash.

  2. Feature Richness: Bash incorporates features from other popular shells like the C shell (csh) and the Korn shell (ksh), while also introducing its own innovations. This makes it a powerful and flexible tool for both interactive use and scripting.

  3. Free Software: Bash is open-source software, released under the GNU General Public License (GPL). This has contributed to its widespread adoption and customization by the open-source community.

  4. Scripting Language: Bash is not only a command interpreter but also a scripting language. This allows users to write complex scripts to automate tasks, manage systems, and perform a wide range of operations.

  5. Default Shell: Bash is the default shell for most Linux distributions and is widely used on macOS and other Unix-like systems. This makes it an essential tool for system administrators, developers, and power users.

Key Features and Contributions

  • Command-line editing: Enhanced command-line editing capabilities, including history, line editing, and tab completion.

  • Job control: Improved job control features, allowing users to manage background processes more effectively.

  • Scripting language: A powerful scripting language with features like variables, control flow structures, and functions.

  • Customization: Extensive customization options, allowing users to tailor their shell environment to their preferences.

  • Community support: A large and active community of users and developers, providing support, documentation, and contributions.

In conclusion, Bash has played a pivotal role in the evolution of Unix and Linux systems. Its compatibility, feature richness, free software nature, scripting capabilities, and widespread adoption have made it an indispensable tool for users and developers alike.

2. Basic Commands

  • Navigating the file-system: cd, ls, pwd

These three commands are fundamental for navigating the file system in Bash. Let's break them down:

1. ls (List)

  • Purpose: Displays a list of files and directories within a specified directory.
  • Usage:
    • ls: Lists the contents of the current directory.

    • ls -l: Lists files in a long format, showing details like permissions, owner, size, and modification time.

    • ls -a: Lists all files, including hidden files (those starting with a dot).

    • ls -h: Displays file sizes in a human-readable format (e.g., 1.5K, 2.3M).

    • ls /path/to/directory: Lists the contents of a specific directory.

2. cd (Change Directory)

  • Purpose: Changes the current working directory.
  • Usage:
    • cd: Changes to the home directory.
    • cd ..: Moves to the parent directory.
    • cd /: Moves to the root directory.
    • cd /path/to/directory: Changes to the specified directory.

3. pwd (Print Working Directory)

  • Purpose: Displays the absolute path of the current working directory.
  • Usage:
    • pwd: Prints the current working directory.

Example Scenario:

Let's say you're in your home directory and want to navigate to a specific file located in Documents/Projects/MyProject/code. Here's how you might do it:

  1. Check current location:

    pwd 
    

    (Output: /home/your_username)

  2. Change to the Documents directory:

    cd Documents
    
  3. List the contents of Documents:

    ls
    
  4. Change to the Projects directory:

    cd Projects
    
  5. List the contents of Projects:

    ls
    
  6. Change to the MyProject directory:

    cd MyProject
    
  7. Change to the code directory:

    cd code
    
  8. Verify your current location:

    pwd 
    

    (Output: /home/your_username/Documents/Projects/MyProject/code)

By combining these commands, you can effectively navigate through your file system and locate the files and directories you need.

  • File operations: cp, mv, rm, touch, mkdir

These commands allow you to manipulate files and directories within your file system.

1. mkdir (Make Directory)

  • Purpose: Creates a new directory.

  • Syntax: mkdir [directory_name]

  • Example:

    mkdir new_folder 
    

    This creates a directory named "new_folder" in the current directory.

2. touch (Create Empty File)

  • Purpose: Creates an empty file or updates the timestamp of an existing file.

  • Syntax: touch [file_name]

  • Example:

    touch my_file.txt 
    

    This creates an empty file named "my_file.txt".

3. cp (Copy)

  • Purpose: Copies files or directories.

  • Syntax: cp [source] [destination]

  • Examples:

    cp my_file.txt backup_folder/ 
    cp -r source_directory/ destination_directory/ 
    
    • The first example copies "my_file.txt" to the "backup_folder" directory.
    • The second example recursively copies the entire "source_directory" to the "destination_directory".

4. mv (Move or Rename)

  • Purpose: Moves or renames files or directories.

  • Syntax: mv [source] [destination]

  • Examples:

    mv old_file.txt new_file.txt 
    mv file_to_move.txt /path/to/destination/ 
    
    • The first example renames "old_file.txt" to "new_file.txt".
    • The second example moves "file_to_move.txt" to the specified destination path.

5. rm (Remove)

  • Purpose: Deletes files or directories.

  • Caution: Use with extreme care, as this command permanently deletes files.

  • Syntax: rm [file_name] or rm -r [directory_name]

  • Examples:

    rm unwanted_file.txt 
    rm -r empty_directory/ 
    
    • The first example deletes "unwanted_file.txt".
    • The second example recursively deletes the "empty_directory" and all its contents.

Important Notes:

  • The -r option with rm is crucial for deleting directories. Without it, rm will only delete empty directories.

  • Always double-check before using rm to avoid accidental data loss.

  • Viewing file contents: cat, less, more, head, tail

These commands allow you to view the contents of files in various ways.

1. cat (Concatenate and Print)

  • Purpose: Displays the entire contents of one or more files to the terminal.

  • Syntax: cat [file1] [file2] ...

  • Example:

    cat my_file.txt 
    cat file1.txt file2.txt 
    

2. less (Page-by-Page Viewer)

  • Purpose: Displays the contents of a file one screen at a time, allowing you to scroll up and down.
  • Syntax: less [file_name]
  • Key Commands:
    • Spacebar: Scroll down one page.
    • b: Scroll up one page.
    • Enter: Scroll down one line.
    • q: Quit less.

3. more (Simple Pager)

  • Purpose: Similar to less, but with fewer navigation options.
  • Syntax: more [file_name]
  • Key Command:
    • Spacebar: Display the next screen.

4. head (Display First Lines)

  • Purpose: Displays the first few lines of a file.

  • Syntax: head -n [number_of_lines] [file_name]

  • Example:

    head -n 5 my_file.txt 
    

    This displays the first 5 lines of "my_file.txt".

5. tail (Display Last Lines)

  • Purpose: Displays the last few lines of a file.

  • Syntax: tail -n [number_of_lines] [file_name]

  • Example:

    tail -n 10 my_file.txt 
    

    This displays the last 10 lines of "my_file.txt".

Choosing the Right Command:

  • Use cat for short files.
  • Use less or more for larger files to avoid overwhelming the terminal.
  • Use head or tail to quickly view the beginning or end of a file.

3. File Permissions

  • Understanding file permissions: chmod, chown, chgrp

File permissions in Unix-like systems control who can read, write, and execute files and directories. These permissions are crucial for system security and data integrity.

Types of Permissions:

  1. Owner: The user who created the file or directory.

  2. Group: A set of users who share access to the file or directory.

  3. Others: All other users on the system who are not the owner or in the group.

Permissions for Each Category:

  • Read (r): Allows the user to view the contents of the file.

  • Write (w): Allows the user to modify or delete the file.

  • Execute (x): Allows the user to run the file as a program (for files) or enter the directory (for directories).

Commands for Managing Permissions:

  1. chmod (Change Mode)

    • Changes the permissions of a file or directory.

    • Symbolic Mode:

      • chmod [who][operator][permission] file

      • who: u (user), g (group), o (others), a (all)

      • operator: + (add), - (remove), = (set)

      • permission: r (read), w (write), x (execute)

      • Example:

        chmod u+x my_script.sh  # Add execute permission for the owner
        chmod g-w data_file.txt # Remove write permission for the group
        
    • Octal Mode:

      • Represents permissions as a three-digit octal number (e.g., 755).

      • Each digit represents permissions for the owner, group, and others, respectively.

      • Example:

        chmod 755 my_executable # Owner: rwx, Group: rx, Others: rx
        
  2. chown (Change Owner)

    • Changes the owner of a file or directory.

    • Syntax: chown [new_owner]:[new_group] file

    • Example:

      chown user1:group1 my_file.txt 
      
  3. chgrp (Change Group)

    • Changes the group ownership of a file or directory.

    • Syntax: chgrp [new_group] file

    • Example:

      chgrp developers my_project_folder 
      

Viewing Permissions:

  • Use the ls -l command to view detailed file information, including permissions.

Important Notes:

  • Changing permissions requires appropriate privileges.

  • Incorrectly changing permissions can affect system stability and data security.

  • Use these commands with caution and always back up important data before making significant changes.

4. Text Processing

  • Using grep, sed, and awk for searching and manipulating text.

1. grep - Powerful Text Searching

  • Core Functionality:

    • Searches for patterns (regular expressions) within files.
    • Displays lines that match the specified pattern.
  • Key Options:

    • -i: Case-insensitive search.
    • -v: Invert match (display lines that do not match).
    • -c: Count the number of matching lines.
    • -l: List only the names of files that contain a match.
  • Example:

    • Find all lines in a file named "access_log" that contain the word "error":

      grep "error" access_log
      
    • Find all lines in all files in the current directory that contain the word "warning" (case-insensitive):

      grep -i "warning" *
      
    • Count the number of lines in "message.txt" that contain the word "success":

      grep -c "success" message.txt
      

2. sed - Stream Editor

  • Core Functionality:

    • Performs a wide range of text transformations.
    • Operates on each line of input.
  • Key Concepts:

    • s/pattern/replacement/flags: The basic substitution command.
      • g: Replace all occurrences on a line.
      • i: Case-insensitive match.
    • Addressing: Specify which lines to operate on (e.g., 1,10 for lines 1-10).
  • Examples:

    • Replace all occurrences of "old_word" with "new_word" in "data.txt":

      sed 's/old_word/new_word/g' data.txt
      
    • Delete the first 10 lines of "data.txt":

      sed '1,10d' data.txt
      

3. awk - Pattern Scanning and Text Processing Language

  • Core Functionality:

    • Powerful tool for parsing and processing text, especially tabular data.
    • Uses a scripting language to define how to handle each line of input.
  • Key Concepts:

    • Fields: Data is typically divided into fields (columns).
    • Records: Each line of input is a record.
    • Built-in variables: $0 (entire line), $1 (first field), $2 (second field), etc.
  • Examples:

    • Print the first column of data in "data.txt" (assuming fields are space-separated):

      awk '{print $1}' data.txt
      
    • Print lines where the value in the second column is greater than 10:

      awk '$2 > 10 {print}' data.txt
      

Combining Commands

  • You can combine these commands using pipes (|) to create powerful text processing pipelines.
  • Example:
    • Find lines containing "error" in "logfile.txt" and then extract the first column (which might be a timestamp) from those lines:

      grep "error" logfile.txt | cut -d ' ' -f 1
      

5. Variables and Environment

  • Defining and using variables.

Variables in Bash are used to store temporary values that can be used within your shell scripts or interactively.

Defining Variables

  • Syntax:

    variable_name=value
    
  • Example:

    name="John Doe"
    age=30
    
  • Important Notes:

    • No spaces around the equal sign (=).
    • Variable names are case-sensitive (name and NAME are different).
    • Variable names can contain letters, numbers, and underscores, but cannot start with a number.

Using Variables

  • Accessing Variable Values:

    echo $name  # Output: John Doe
    
  • Using Variables in Commands:

    mkdir $name_directory 
    
  • Concatenating Strings:

    greeting="Hello, "
    echo $greeting$name 
    

Special Variables

  • $0: The name of the current script.
  • $1, $2, $3, ...: Command-line arguments passed to the script.
  • $#: The number of command-line arguments.
  • $?: The exit status of the last command executed.
  • $PWD: The current working directory.
  • $HOME: The home directory of the current user.

Example Script:

#!/bin/bash

name="Alice"
age=25

echo "Name: $name"
echo "Age: $age"

greeting="Hello, $name!"
echo $greeting

This script demonstrates how to define variables and use them within an echo command.

6. Control Structures

  • Conditional statements: if, else, elif, case

Conditional statements allow you to control the flow of execution in your Bash scripts based on certain conditions. Here's a breakdown of the key conditional statements:

1. if Statement

  • Basic Structure:
if [ condition ]; then
  commands_to_execute_if_true
fi
  • Example:
if [ -f "myfile.txt" ]; then
  echo "File 'myfile.txt' exists."
fi

This checks if the file "myfile.txt" exists. If it does, the message "File 'myfile.txt' exists." is printed to the console.

2. if-else Statement

  • Basic Structure:
if [ condition ]; then
  commands_to_execute_if_true
else
  commands_to_execute_if_false
fi
  • Example:
if [ "$USER" = "root" ]; then
  echo "You are the root user."
else
  echo "You are not the root user."
fi

This checks if the current user is "root." If it is, the first message is printed; otherwise, the second message is printed.

3. if-elif-else Statement

  • Basic Structure:
if [ condition1 ]; then
  commands_to_execute_if_condition1_is_true
elif [ condition2 ]; then 
  commands_to_execute_if_condition2_is_true
else
  commands_to_execute_if_no_conditions_are_true
fi
  • Example:
read -p "Enter a number: " num

if [ $num -gt 0 ]; then
  echo "The number is positive."
elif [ $num -lt 0 ]; then
  echo "The number is negative."
else
  echo "The number is zero."
fi

This script prompts the user for a number and then determines whether it's positive, negative, or zero.

4. case Statement

  • Basic Structure:
case "$variable" in
  pattern1)
    commands_to_execute_if_variable_matches_pattern1;;
  pattern2)
    commands_to_execute_if_variable_matches_pattern2;;
  *)
    commands_to_execute_if_no_pattern_matches;;
esac
  • Example:
read -p "Enter a day of the week: " day

case "$day" in
  "Monday")
    echo "It's the start of the week.";;
  "Friday")
    echo "It's almost the weekend!";;
  "Saturday" | "Sunday")
    echo "Happy weekend!";;
  *)
    echo "It's just another day.";;
esac

This script prompts the user for a day of the week and then displays an appropriate message based on the input.

Key Considerations:

  • Use proper indentation to improve code readability.
  • Test your scripts thoroughly to ensure they work as expected under various conditions.
  • Consider using functions to break down complex logic into smaller, more manageable units.

7. Functions

  • Defining and calling functions in Bash.

Functions in Bash are reusable blocks of code that perform a specific task. They improve code organization, readability, and maintainability by encapsulating related operations.

Defining a Function

  • Syntax:
function function_name() {
  commands
}

# or (shorter syntax)

function_name() {
  commands
}
  • Example:
function greet() {
  echo "Hello, world!"
}

This defines a function named greet that simply prints "Hello, world!" to the console.

Calling a Function

To execute a function, simply use its name followed by parentheses:

Passing Arguments to a Function

Functions can accept arguments, which are values passed to the function when it's called.

  • Accessing Arguments:

    • $1: The first argument.
    • $2: The second argument.
    • $3: The third argument, and so on.
    • $@: All arguments as a single word.
    • $*: All arguments as a single string.
  • Example:

function greet_user() {
  echo "Hello, $1!"
}

greet_user "John Doe" 

This defines a function greet_user that takes one argument (the user's name) and prints a personalized greeting.

Returning Values from a Function

Bash functions typically return values implicitly through their output. However, you can also use the return statement to return a specific integer value. This value can be checked by other parts of the script.

Example:

function add() {
  local sum=$(( $1 + $2 ))
  return $sum
}

result=$(add 5 3) 
echo "The sum is: $result" 

Key Considerations:

  • Use meaningful function names to improve code readability.
  • Break down complex tasks into smaller, well-defined functions.
  • Avoid overly long or complex functions.
  • Use comments to document the purpose and behavior of your functions.

Example:

#!/bin/bash

function calculate_area() {
  local radius=$1
  local area=$(echo "scale=2; 3.14159 * $radius * $radius" | bc)
  echo "The area of the circle is: $area"
}

read -p "Enter the radius: " radius

calculate_area $radius

This script defines a function calculate_area that calculates the area of a circle and then uses it to calculate the area based on user input.

8. Scripting Basics

  • Writing and executing a simple Bash script.

1. Create a File

  • Use a text editor (like Nano, Vim, or Emacs) to create a new file. For example, save it as hello.sh.

2. Add the Shebang

  • The first line of the script should be the "shebang," which tells the system which interpreter to use. For Bash, it's:
#!/bin/bash

3. Write the Script

  • Add the following line to the file:
echo "Hello, world!"

4. Save the File

  • Save the file.

5. Make the Script Executable

  • Open a terminal and navigate to the directory where you saved the script.
  • Use the chmod command to make the script executable:
chmod +x hello.sh

6. Execute the Script

  • Run the script by typing its name in the terminal:
./hello.sh

This will output:

Hello, world!

Explanation

  • The #!/bin/bash line tells the system to use the Bash interpreter to execute the script.
  • The echo command prints the message "Hello, world!" to the terminal.
  • The chmod +x command makes the script file executable.

Example with a Variable

Here's a slightly more complex example:

#!/bin/bash

name="Alice"
echo "Hello, $name!"

This script defines a variable name and then prints a personalized greeting.

This is a basic example, but it demonstrates the fundamental steps involved in writing and executing a Bash script. You can expand on these concepts to create more sophisticated scripts for various tasks.

9. Input and Output

  • Redirecting input and output: >, >>, <, |

1. Output Redirection (> and >>)

  • > (Overwrite):

    • Directs the standard output of a command to a file.
    • Overwrites any existing content in the file.
    ls -l > file_list.txt 
    

    This command lists files in long format (ls -l) and redirects the output to a file named file_list.txt. If file_list.txt already exists, its contents will be replaced.

  • >> (Append):

    • Directs the standard output of a command to a file.
    • Appends the output to the end of the file, preserving any existing content.
    date >> system_log.txt
    

    This command displays the current date and time (date) and appends the output to a file named system_log.txt.

2. Input Redirection (<)

  • Directs the standard input of a command to read data from a file instead of the keyboard.

    wc -l < my_file.txt
    

    This command counts the number of lines (wc -l) in the file my_file.txt.

3. Pipelines (|)

  • Connects the standard output of one command to the standard input of another command.

    ls -l | grep "my_file"
    

    This command first lists files in long format (ls -l) and then pipes the output to the grep command, which searches for lines containing "my_file".

Example:

cat my_file.txt | grep "keyword" | wc -l > results.txt

This command:

  1. Reads the contents of my_file.txt (cat my_file.txt).
  2. Pipes the output to grep to find lines containing "keyword".
  3. Pipes the output of grep to wc -l to count the number of matching lines.
  4. Redirects the final output (the line count) to a file named results.txt.