Lesson 4.1: Bash Introduction

Bash (Bourne Again Shell) is the default command-line interface on most Linux systems, including Ubuntu. It's a powerful tool that lets you interact with your system through text commands.

Why Bash? Bash combines the power of traditional Unix shells with modern features like command completion, history, and scripting capabilities. It's the foundation for Linux system administration and development.

Opening a Terminal

On Ubuntu 24.04, you have several ways to open a terminal:

Method 1: Keyboard Shortcut

Press Ctrl + Alt + T - this is the fastest way to open a terminal.

Method 2: Application Menu

Method 3: Right-Click Menu

Right-click on your desktop and select "Open in Terminal" (if available).

Understanding the Prompt

When you open a terminal, you'll see something like this:

valente@ubuntu:~$

Let's break down what this means:

Prompt Characters: $ means regular user, # means root/superuser. If you see #, be extra careful with your commands!

Your First Commands

Let's start with some basic commands to get familiar with the terminal:

Check Who You Are

$ whoami valente

See Where You Are

$ pwd /home/valente

List Files in Current Directory

$ ls Desktop Documents Downloads Music Pictures Public Templates Videos

See Current Date and Time

$ date Mon Dec 10 15:45:32 EST 2024

Command Structure

Linux commands follow a basic structure:

command [options] [arguments]

Example: ls with options

$ ls -l /home total 4 drwxr-xr-x 15 valente valente 4096 Dec 10 15:45 valente

Here: ls is the command, -l is an option (long format), and /home is an argument (directory to list).

Navigation Commands

Moving around the filesystem is fundamental to using the command line:

Change Directory (cd)

$ cd Documents # Go to Documents directory $ pwd /home/valente/Documents $ cd .. # Go up one directory $ pwd /home/valente $ cd ~ # Go to home directory $ pwd /home/valente $ cd / # Go to root directory $ pwd /

Special Directory References

File Operations

Basic file management commands:

Create Files

$ touch myfile.txt # Create empty file $ echo "Hello World" > hello.txt # Create file with content

View Files

$ cat hello.txt # Show file contents Hello World $ less hello.txt # View file page by page (press q to quit)

Copy and Move Files

$ cp hello.txt backup.txt # Copy file $ mv hello.txt newname.txt # Rename file $ mv newname.txt Documents/ # Move file to directory

Remove Files

$ rm backup.txt # Remove file (be careful!) $ rm -rf directory/ # Remove directory and contents (very careful!)
Safety First: The rm command is permanent! There's no recycle bin or undo. Always double-check your commands, especially with rm -rf.

Bash Features That Make Life Easier

Tab Completion

Type part of a command or filename and press Tab to complete it:

$ cd Doc[Tab] # Completes to "Documents" $ ls myfi[Tab] # Completes to "myfile.txt"

Command History

Use arrow keys to navigate previous commands:

Clear Screen

$ clear # Clear the screen # Or use Ctrl + L

Getting Help

Linux has built-in help systems:

man Pages

$ man ls # Manual page for ls command $ man pwd # Manual page for pwd command

--help Flag

$ ls --help # Quick help for ls $ cp --help # Quick help for cp

what is and apropos

$ whatis ls # Brief description of command ls - list directory contents $ apropos "list files" # Find commands related to topic ls (1) - list directory contents tree (1) - list contents of directories in a tree-like format

Command Line Shortcuts

These shortcuts will save you time:

Working with Multiple Commands

Command Chaining

$ pwd; ls # Run multiple commands $ cd Documents && ls # Run second only if first succeeds $ cd nowhere || echo "Directory not found" # Run second if first fails

Background Processes

$ sleep 10 & # Run command in background [1] 12345 $ jobs # Show background jobs [1]+ Running sleep 10 & $ fg # Bring job to foreground

Environment Variables

Environment variables store system information:

$ echo $HOME # Your home directory /home/valente $ echo $PATH # Command search path /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin $ echo $USER # Your username valente

Customizing Your Shell

You can customize Bash using configuration files:

.bashrc File

The ~/.bashrc file runs every time you open a terminal. You can add:

Example .bashrc additions

# Aliases alias ll='ls -alF' alias la='ls -A' alias l='ls -CF' # Custom prompt export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Practice Exercises

Try these exercises to practice what you've learned:

Exercise 1: Navigation Practice

  1. Navigate to your Documents directory
  2. Create a new directory called "practice"
  3. Go into that directory
  4. Create a file called "test.txt"
  5. Go back to your home directory
  6. Remove the practice directory and its contents
Click for solution
$ cd Documents $ mkdir practice $ cd practice $ touch test.txt $ cd ~ $ rm -rf Documents/practice

Exercise 2: File Management

  1. Create three files: file1.txt, file2.txt, file3.txt
  2. Copy file1.txt to backup.txt
  3. Rename file2.txt to renamed.txt
  4. Move file3.txt to a new directory called "moved"
  5. List all files to verify your work
Click for solution
$ touch file1.txt file2.txt file3.txt $ cp file1.txt backup.txt $ mv file2.txt renamed.txt $ mkdir moved $ mv file3.txt moved/ $ ls -la

What's Next?

Now that you understand Bash basics, let's explore terminal applications that will make you more productive!

Continue to Terminal Applications

Key Takeaways

  • Bash is the default shell on Ubuntu and most Linux systems
  • Commands follow the structure: command [options] [arguments]
  • Tab completion saves time and prevents typos
  • Command history lets you reuse previous commands
  • Use man and --help to learn about commands
  • Always be careful with rm - there's no undo!