Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md #112

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 115 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<p align="center">
<img src="https://cloud.githubusercontent.com/assets/2059754/24601246/753a7f36-1858-11e7-9d6b-7a0e64fb27f7.png" alt="bash logo"/>
</p>
<div>This comprehensive guide provides an introduction to Bash scripting and command-line operations. Whether you're a beginner or an experienced user, this guide offers valuable insights into Bash syntax, shell programming, and useful tricks to enhance your command-line productivity.

## Who is this guide for?

- Beginners looking to learn Bash scripting
- Intermediate users wanting to improve their shell programming skills
- Advanced users seeking a quick reference or new tricks

Let's dive in and explore the power of Bash!</div>

## Table of Contents
1. [Basic Operations](#1-basic-operations)
1.1. [File Operations](#11-file-operations)
1.1. [File Operations](#11-file-operations)
1.2. [Text Operations](#12-text-operations)
1.3. [Directory Operations](#13-directory-operations)
1.4. [SSH, System Info & Network Operations](#14-ssh-system-info--network-operations)
1.5. [Process Monitoring Operations](#15-process-monitoring-operations)
2. [Basic Shell Programming](#2-basic-shell-programming)
3. [Basic Shell Programming](#2-basic-shell-programming)
2.1. [Variables](#21-variables)
2.2. [Array](#22-array)
2.3. [String Substitution](#23-string-substitution)
Expand All @@ -19,62 +28,67 @@
2.7. [Loops](#27-loops)
2.8. [Regex](#28-regex)
2.9. [Pipes](#29-pipes)
3. [Tricks](#3-tricks)
4. [Debugging](#4-debugging)
5. [Multi-threading](#5-multi-threading)
4. [Tricks](#3-tricks)
5. [Debugging](#4-debugging)
6. [Multi-threading](#5-multi-threading)

# 1. Basic Operations

### a. `export`
Displays all environment variables. If you want to get details of a specific variable, use `echo $VARIABLE_NAME`.
```bash
Displays all environment variables. To see the value of a specific variable, use `echo $VARIABLE_NAME`.

Usage:
export
```

Example:
```bash
$ export
AWS_HOME=/Users/adnanadnan/.aws
AWS_HOME=/Users/yourname/.aws
LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LESS=-R
```

$ echo $AWS_HOME
/Users/adnanadnan/.aws
```
/Users/yourname/.aws

### b. `whatis`
whatis shows description for user commands, system calls, library functions, and others in manual pages
```bash
whatis something
```
Shows a brief description for user commands, system calls, library functions, and others found in manual pages.

Usage:
whatis command_name

Example:
```bash
$ whatis bash
bash (1) - GNU Bourne-Again SHell
```

### c. `whereis`
whereis searches for executables, source files, and manual pages using a database built by system automatically.
```bash
Searches for executables, source files, and manual pages using a database built by the system.

Usage:
whereis name
```

Example:
```bash
$ whereis php
/usr/bin/php
```

### d. `which`
which searches for executables in the directories specified by the environment variable PATH. This command will print the full path of the executable(s).
```bash
which program_name
```
Searches for executables in the directories specified by the `PATH` environment variable and prints the full path.

Usage:
which program_name

Example:
```bash
$ which php
/c/xampp/php/php
```


### e. clear
Clears content on window.

Expand Down Expand Up @@ -132,34 +146,52 @@ The chown command stands for "change owner", and allows you to change the owner
chown -options user:group filename
```

### d. `cp`
Copies a file from one location to other.
# d. cp
Copies a file from one location to another.

Usage:
```bash
cp filename1 filename2
```
Where `filename1` is the source path to the file and `filename2` is the destination path to the file.

### e. `diff`
Compares files, and lists their differences.
Where `filename1` is the source file path and `filename2` is the destination file path.


# e. diff
Compares two files and lists their differences.

Usage:
```bash
diff filename1 filename2
```

### f. `file`
Determine file type.
# f. file
Determines the file type.

Usage:
```bash
file filename
```
Example:

Example:
```bash
$ file index.html
index.html: HTML document, ASCII text
index.html: HTML document, ASCII text
```
### g. `find`
Find files in directory


# g. find
Finds files in a directory.

Usage:
```bash
find directory options pattern
```

Examples:
```bash
$ find . -name README.md
$ find /home/user1 -name '*.png'
Example:
```bash
$ find . -name README.md
Expand Down Expand Up @@ -972,27 +1004,67 @@ nohup command &

# 2. Basic Shell Programming

Bash scripting allows you to automate tasks and create powerful command-line tools. This section covers the fundamental concepts of shell programming.

The first line that you will write in bash script files is called `shebang`. This line in any script determines the script's ability to be executed like a standalone executable without typing sh, bash, python, php etc beforehand in the terminal.
## 2.1.Variables

The first line of a Bash script, known as the "shebang", determines how the script should be executed:

```bash
#!/usr/bin/env bash
```

## 2.1. Variables
## 2.2. Variables

Creating variables in bash is similar to other languages. There are no data types. A variable in bash can contain a number, a character, a string of characters, etc. You have no need to declare a variable, just assigning a value to its reference will create it.
In Bash, variables are used to store data. Unlike many programming languages, Bash doesn't require you to declare a variable's type. Here's how to work with variables:

### Creating Variables

To create a variable, simply assign a value to a name:

Example:
```bash
str="hello world"
```
name="John Doe"
age=30
PI=3.14159

The above line creates a variable `str` and assigns "hello world" to it. The value of variable is retrieved by putting the `$` in the beginning of variable name.
### Creating and Using Variables

To create a variable, assign a value to a name. To use a variable, prefix its name with a dollar sign ($):

Example:
```bash
echo $str # hello world
# Creating variables
name="John Doe"
age=30
PI=3.14159

# Using variables
echo "Hello, $name! You are $age years old."
echo "Pi is approximately $PI"

# Command substitution
current_date=$(date +%Y-%m-%d)
file_count=$(ls | wc -l)

# Arithmetic operations
x=5
y=3
sum=$((x + y))
product=$((x * y))

# Read-only variables
readonly CONSTANT_VALUE=100

# Unsetting variables
unset variable_name

# Local variables in functions
function example_function() {
local local_var="I'm local"
echo "$local_var"
}

# Always quote your variables when using them
file_name="My Document.txt"
cat "$file_name" # Correct
```
## 2.2. Array
Like other languages bash has also arrays. An array is a variable containing multiple values. There's no maximum limit on the size of array. Arrays in bash are zero based. The first element is indexed with element 0. There are several ways for creating arrays in bash which are given below.
Expand Down