Skip to content

1.3 Bash

Bash (Bourne Again Shell) is a command-line interpreter for Linux and other Unix-like systems. It allows users to interact with the operating system through typed commands and scripts. This tutorial will introduce you to the basics of Bash, including common commands, file manipulation, and scripting.

1. Basic Bash Commands

1.1 Navigating the Filesystem

  • pwd: Print the current working directory (shows where you are).

    Terminal window
    pwd
  • ls: List files and directories in the current directory.

    • ls -l: List files with detailed information (permissions, file size, etc.).
    • ls -a: Show all files, including hidden files (those starting with .).
    Terminal window
    ls
    ls -l
    ls -a
  • cd: Change directory.

    Terminal window
    cd /path/to/directory # Go to a specific directory
    cd .. # Go up one directory
    cd ~ # Go to the home directory

1.2 File Operations

  • cp: Copy files or directories.

    Terminal window
    cp file1.txt file2.txt # Copy file1.txt to file2.txt
    cp -r dir1/ dir2/ # Copy directory dir1 to dir2 (recursive)
  • mv: Move or rename files or directories.

    Terminal window
    mv oldfile.txt newfile.txt # Rename file
    mv file.txt /path/to/destination/ # Move file to a new directory
  • rm: Remove files or directories.

    Terminal window
    rm file.txt # Remove a file
    rm -r dir/ # Remove a directory and its contents (recursive)
    rm -f file.txt # Force remove without confirmation
  • touch: Create an empty file or update the timestamp of an existing file.

    Terminal window
    touch newfile.txt # Create an empty file named "newfile.txt"

1.3 Viewing File Contents

  • cat: Concatenate and display the contents of a file.

    Terminal window
    cat file.txt # Display the content of file.txt
  • less: View large files one page at a time. You can scroll through the file.

    Terminal window
    less file.txt
  • head: Show the first 10 lines of a file.

    Terminal window
    head file.txt
  • tail: Show the last 10 lines of a file.

    Terminal window
    tail file.txt
    tail -f file.txt # Follow the file (useful for log files)

2. File Permissions and Ownership

  • chmod: Change file permissions.

    • chmod 755 file.txt: Sets read/write/execute for owner, and read/execute for group and others.
    • chmod +x script.sh: Adds executable permission to the script.

    Permission notation:

    • r = read
    • w = write
    • x = execute

    Numeric representation:

    • 7 = rwx (read, write, execute)
    • 6 = rw-
    • 5 = r-x
    • 4 = r--
    Terminal window
    chmod 644 file.txt # rw-r--r--
    chmod 755 file.txt # rwxr-xr-x
  • chown: Change file owner and group.

    Terminal window
    chown user:group file.txt # Change owner and group
  • chgrp: Change group ownership.

    Terminal window
    chgrp group file.txt # Change group ownership

3. Variables and Input

3.1. Setting and Using Variables

  • Define a variable:

    Terminal window
    VAR_NAME="Hello, world!"
  • Access the value of a variable:

    Terminal window
    echo $VAR_NAME # Output: Hello, world!
  • Use variables in commands:

    Terminal window
    FOLDER="/home/user/Documents"
    cd $FOLDER # Change directory to /home/user/Documents

3.2. User Input

  • Read input from the user:
    Terminal window
    read -p "Enter your name: " USER_NAME
    echo "Hello, $USER_NAME!"

4. Basic Bash Scripting

A Bash script is a text file containing a sequence of commands. It can automate tasks and execute commands in sequence.

4.1. Writing a Simple Script

  1. Create a file with a .sh extension, e.g., hello.sh.
  2. Add the following to the script:
#!/bin/bash
# This is a simple script
echo "Hello, World!"
  • Explanation:
    • #!/bin/bash: This is called the shebang and tells the system which interpreter to use (in this case, Bash).
    • echo: Prints text to the terminal.
  1. Give the script execute permission:
Terminal window
chmod +x hello.sh
  1. Run the script:
Terminal window
./hello.sh

4.2. Variables in Scripts

#!/bin/bash
# Script to greet the user
echo "Enter your name:"
read name
echo "Hello, $name!"

4.3. Conditional Statements

You can use if statements to make decisions in your script.

#!/bin/bash
echo "Enter a number:"
read num
if [ $num -gt 10 ]; then
echo "The number is greater than 10"
else
echo "The number is 10 or less"
fi
  • Explanation:
    • -gt: Greater than.
    • if [ $num -gt 10 ]: Checks if the value of num is greater than 10.

4.4. Loops

You can use for, while, and until loops in Bash scripts.

For Loop*

#!/bin/bash
for i in {1..5}; do
echo "Iteration $i"
done

While Loop

#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Iteration $count"
((count++)) # Increment count
done

Until Loop*

#!/bin/bash
count=1
until [ $count -gt 5 ]; do
echo "Iteration $count"
((count++)) # Increment count
done

4.5. Functions

Bash allows you to create reusable functions.

#!/bin/bash
greet() {
echo "Hello, $1!" # $1 is the first argument passed to the function
}
greet "Alice"
greet "Bob"

5. Advanced Bash Commands

  • grep: Search for patterns in files.

    Terminal window
    grep "pattern" file.txt # Search for "pattern" in file.txt
  • find: Search for files in a directory hierarchy.

    Terminal window
    find /path/to/dir -name "*.txt" # Find all .txt files in a directory
  • awk: Text processing tool.

    Terminal window
    awk '{print $1}' file.txt # Print the first column of each line in file.txt
  • sed: Stream editor for basic text transformations.

    Terminal window
    sed 's/old/new/' file.txt # Replace "old" with "new" in file.txt
  • tar: Archive files.

    Terminal window
    tar -czf archive.tar.gz directory/ # Create a compressed archive
    tar -xzf archive.tar.gz # Extract a compressed archive