diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
new file mode 100644
index 0000000..0be85e1
--- /dev/null
+++ b/.github/FUNDING.yml
@@ -0,0 +1,4 @@
+# These are supported funding model platforms
+
+github: [idnan]
+custom: https://paypal.me/id9a9
diff --git a/README.md b/README.md
index cd8e321..0f9ad4d 100644
--- a/README.md
+++ b/README.md
@@ -8,15 +8,16 @@
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 (TODO)](#15-process-monitoring-operations)
+ 1.5. [Process Monitoring Operations](#15-process-monitoring-operations)
2. [Basic Shell Programming](#2-basic-shell-programming)
2.1. [Variables](#21-variables)
- 2.3. [String Substitution](#22-string-substitution)
- 2.4. [Functions](#23-functions)
- 2.5. [Conditionals](#24-conditionals)
- 2.6. [Loops](#25-loops)
- 3. [Tricks](#4-tricks)
- 4. [Debugging](#5-debugging)
+ 2.2. [Array](#22-array)
+ 2.3. [String Substitution](#23-string-substitution)
+ 2.4. [Functions](#24-functions)
+ 2.5. [Conditionals](#25-conditionals)
+ 2.6. [Loops](#26-loops)
+ 3. [Tricks](#3-tricks)
+ 4. [Debugging](#4-debugging)
# 1. Basic Operations
@@ -29,17 +30,27 @@ export
Example:
```bash
$ export
-SHELL=/bin/zsh
AWS_HOME=/Users/adnanadnan/.aws
LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LESS=-R
-$ echo $SHELL
-/usr/bin/zsh
+$ echo $AWS_HOME
+/Users/adnanadnan/.aws
```
-### b. `whereis`
+### b. `whatis`
+whatis shows description for user commands, system calls, library functions, and others in manual pages
+```bash
+whatis something
+```
+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
whereis name
@@ -50,7 +61,7 @@ $ whereis php
/usr/bin/php
```
-### c. `which`
+### 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
@@ -61,63 +72,38 @@ $ which php
/c/xampp/php/php
```
-### d. clear
+### e. clear
Clears content on window.
## 1.1. File Operations
-### a. `ls`
-Lists your files. `ls` has many options: `-l` lists files in 'long format', which contains the exact size of the file, who owns the file, who has the right to look at it, and when it was last modified. `-a` lists all files, including hidden files. For more information on this command check this [link](https://ss64.com/bash/ls.html).
-```bash
-ls option
-```
-Example:
-
-$ ls -al
-rwxr-xr-x 33 adnan staff 1122 Mar 27 18:44 .
-drwxrwxrwx 60 adnan staff 2040 Mar 21 15:06 ..
--rw-r--r--@ 1 adnan staff 14340 Mar 23 15:05 .DS_Store
--rw-r--r-- 1 adnan staff 157 Mar 25 18:08 .bumpversion.cfg
--rw-r--r-- 1 adnan staff 6515 Mar 25 18:08 .config.ini
--rw-r--r-- 1 adnan staff 5805 Mar 27 18:44 .config.override.ini
-drwxr-xr-x 17 adnan staff 578 Mar 27 23:36 .git
--rwxr-xr-x 1 adnan staff 2702 Mar 25 18:08 .gitignore
-
-
-### b. `touch`
-Creates or updates your file.
-```bash
-touch filename
-```
-Example:
-```bash
-$ touch trick.md
-```
-
-### c. `cat`
+### a. `cat`
It can be used for the following purposes under UNIX or Linux.
* Display text files on screen
* Copy text files
@@ -127,86 +113,80 @@ It can be used for the following purposes under UNIX or Linux.
cat filename
cat file1 file2
cat file1 file2 > newcombinedfile
+cat < file1 > file2 #copy file1 to file2
```
-### d. `more`
-Shows the first part of a file (move with space and type q to quit).
-```bash
-more filename
-```
-
-### e. `head`
-Outputs the first 10 lines of file
-```bash
-head filename
-```
-
-### f. `tail`
-Outputs the last 10 lines of file. Use `-f` to output appended data as the file grows.
+### b. `chmod`
+The chmod command stands for "change mode" and allows you to change the read, write, and execute permissions on your files and folders. For more information on this command check this [link](https://ss64.com/bash/chmod.html).
```bash
-tail filename
+chmod -options filename
```
-
-### g. `mv`
-Moves a file from one location to other.
+### c. `chown`
+The chown command stands for "change owner", and allows you to change the owner of a given file or folder, which can be a user and a group. Basic usage is simple forward first comes the user (owner), and then the group, delimited by a colon.
```bash
-mv filename1 filename2
+chown -options user:group filename
```
-Where `filename1` is the source path to the file and `filename2` is the destination path to the file.
-### h. `cp`
+### d. `cp`
Copies a file from one location to other.
```bash
cp filename1 filename2
```
Where `filename1` is the source path to the file and `filename2` is the destination path to the file.
-### i. `rm`
-Removes a file. Using this command on a directory gives you an error.
-`rm: directory: is a directory`
-In order to remove a directory you have to pass `-rf` to remove all the content of the directory recursively.
-```bash
-rm filename
-```
-
-### j. `diff`
+### e. `diff`
Compares files, and lists their differences.
```bash
diff filename1 filename2
```
-### k. `chmod`
-Lets you change the read, write, and execute permissions on your files.
+### f. `file`
+Determine file type.
```bash
-chmod -options filename
+file filename
```
-
-### l. `gzip`
-Compresses files.
+Example:
```bash
-gzip filename
+$ file index.html
+ index.html: HTML document, ASCII text
+```
+### g. `find`
+Find files in directory
+```bash
+find directory options pattern
+```
+Example:
+```bash
+$ find . -name README.md
+$ find /home/user1 -name '*.png'
```
-### m. `gunzip`
+### h. `gunzip`
Un-compresses files compressed by gzip.
```bash
gunzip filename
```
-### n. `gzcat`
+### i. `gzcat`
Lets you look at gzipped file without actually having to gunzip it.
```bash
gzcat filename
```
-### o. `lpr`
-Print the file.
+### j. `gzip`
+Compresses files.
```bash
-lpr filename
+gzip filename
```
-### p. `lpq`
+### k. `head`
+Outputs the first 10 lines of file
+```bash
+head filename
+```
+
+### l. `lpq`
Check out the printer queue.
```bash
lpq
@@ -219,31 +199,97 @@ active adnanad 59 demo 399360 bytes
1st adnanad 60 (stdin) 0 bytes
```
-### q. `lprm`
+### m. `lpr`
+Print the file.
+```bash
+lpr filename
+```
+
+### n. `lprm`
Remove something from the printer queue.
```bash
lprm jobnumber
```
+### o. `ls`
+Lists your files. `ls` has many options: `-l` lists files in 'long format', which contains the exact size of the file, who owns the file, who has the right to look at it, and when it was last modified. `-a` lists all files, including hidden files. For more information on this command check this [link](https://ss64.com/bash/ls.html).
+```bash
+ls option
+```
+Example:
+
+$ ls -la
+rwxr-xr-x 33 adnan staff 1122 Mar 27 18:44 .
+drwxrwxrwx 60 adnan staff 2040 Mar 21 15:06 ..
+-rw-r--r--@ 1 adnan staff 14340 Mar 23 15:05 .DS_Store
+-rw-r--r-- 1 adnan staff 157 Mar 25 18:08 .bumpversion.cfg
+-rw-r--r-- 1 adnan staff 6515 Mar 25 18:08 .config.ini
+-rw-r--r-- 1 adnan staff 5805 Mar 27 18:44 .config.override.ini
+drwxr-xr-x 17 adnan staff 578 Mar 27 23:36 .git
+-rwxr-xr-x 1 adnan staff 2702 Mar 25 18:08 .gitignore
+
+
+### p. `more`
+Shows the first part of a file (move with space and type q to quit).
+```bash
+more filename
+```
+
+### q. `mv`
+Moves a file from one location to other.
+```bash
+mv filename1 filename2
+```
+Where `filename1` is the source path to the file and `filename2` is the destination path to the file.
+
+Also it can be used for rename a file.
+```bash
+mv old_name new_name
+```
+
+### r. `rm`
+Removes a file. Using this command on a directory gives you an error.
+`rm: directory: is a directory`
+To remove a directory you have to pass `-r` which will remove the content of the directory recursively. Optionally you can use `-f` flag to force the deletion i.e. without any confirmations etc.
+```bash
+rm filename
+```
+
+### s. `tail`
+Outputs the last 10 lines of file. Use `-f` to output appended data as the file grows.
+```bash
+tail filename
+```
+
+### t. `touch`
+Updates access and modification time stamps of your file. If it doesn't exists, it'll be created.
+```bash
+touch filename
+```
+Example:
+```bash
+$ touch trick.md
+```
+
## 1.2. Text Operations
@@ -276,177 +322,122 @@ sync
```
For more detail on how to use `awk`, check following [link](https://www.cyberciti.biz/faq/bash-scripting-using-awk).
-### b. `grep`
-Looks for text inside files. You can use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.
-```bash
-grep pattern filename
-```
-Example:
-```bash
-$ grep admin /etc/passwd
-_kadmin_admin:*:218:-2:Kerberos Admin Service:/var/empty:/usr/bin/false
-_kadmin_changepw:*:219:-2:Kerberos Change Password Service:/var/empty:/usr/bin/false
-_krb_kadmin:*:231:-2:Open Directory Kerberos Admin Service:/var/empty:/usr/bin/false
-```
-You can also force grep to ignore word case by using `-i` option. `-r` can be used to search all files under the specified directory, for example:
-```bash
-$ grep -r admin /etc/
-```
-And `-w` to search for words only. For more detail on `grep`, check following [link](https://www.cyberciti.biz/faq/grep-in-bash).
-
-### c. `wc`
-Tells you how many lines, words and characters there are in a file.
-```bash
-wc filename
-```
-Example:
-```bash
-$ wc demo.txt
-7459 15915 398400 demo.txt
-```
-Where `7459` is lines, `15915` is words and `398400` is characters.
-### d. `sed`
-Stream editor for filtering and transforming text
+### b. `cut`
+Remove sections from each line of files
*example.txt*
```bash
-Hello This is a Test 1 2 3 4
-```
-
-*replace all spaces with hyphens*
-```bash
-sed 's/ /-/g' example.txt
-```
-```bash
-Hello-This-is-a-Test-1-2-3-4
+red riding hood went to the park to play
```
-*replace all digits with "d"*
-```bash
-sed 's/[0-9]/d/g' example.txt
-```
+*show me columns 2 , 7 , and 9 with a space as a separator*
```bash
-Hello This is a Test d d d d
+cut -d " " -f2,7,9 example.txt
```
-
-### e. `sort`
-Sort lines of text files
-
-*example.txt*
```bash
-f
-b
-c
-g
-a
-e
-d
+riding park play
```
+### c. `echo`
+Display a line of text
-*sort example.txt*
+*display "Hello World"*
```bash
-sort example.txt
+echo Hello World
```
```bash
-a
-b
-c
-d
-e
-f
-g
+Hello World
```
-*randomize a sorted example.txt*
+*display "Hello World" with newlines between words*
```bash
-sort example.txt | sort -R
+echo -ne "Hello\nWorld\n"
```
```bash
-b
-f
-a
-c
-d
-g
-e
+Hello
+World
```
-### f. `uniq`
-Report or omit repeated lines
+### d. `egrep`
+Print lines matching a pattern - Extended Expression (alias for: 'grep -E')
*example.txt*
```bash
-a
-a
-b
-a
-b
-c
-d
-c
-```
-
-*show only unique lines of example.txt (first you need to sort it, otherwise it won't see the overlap)*
-```bash
-sort example.txt | uniq
-```
-```bash
-a
-b
-c
-d
+Lorem ipsum
+dolor sit amet,
+consetetur
+sadipscing elitr,
+sed diam nonumy
+eirmod tempor
+invidunt ut labore
+et dolore magna
+aliquyam erat, sed
+diam voluptua. At
+vero eos et
+accusam et justo
+duo dolores et ea
+rebum. Stet clita
+kasd gubergren,
+no sea takimata
+sanctus est Lorem
+ipsum dolor sit
+amet.
```
-*show the unique items for each line, and tell me how many instances it found*
+*display lines that have either "Lorem" or "dolor" in them.*
```bash
-sort example.txt | uniq -c
+egrep '(Lorem|dolor)' example.txt
+or
+grep -E '(Lorem|dolor)' example.txt
```
```bash
- 3 a
- 2 b
- 2 c
- 1 d
+Lorem ipsum
+dolor sit amet,
+et dolore magna
+duo dolores et ea
+sanctus est Lorem
+ipsum dolor sit
```
-### g. `cut`
-Remove sections from each line of files
+### e. `fgrep`
+Print lines matching a pattern - FIXED pattern matching (alias for: 'grep -F')
*example.txt*
```bash
-red riding hood went to the park to play
-```
-
-*show me columns 2 , 7 , and 9 with a space as a separator*
-```bash
-cut -d " " -f2,7,9 example.txt
-```
-```bash
-riding park play
-```
-
-### h. `echo`
-Display a line of text
-
-*display "Hello World"*
-```bash
-echo Hello World
-```
-```bash
-Hello World
+Lorem ipsum
+dolor sit amet,
+consetetur
+sadipscing elitr,
+sed diam nonumy
+eirmod tempor
+foo (Lorem|dolor)
+invidunt ut labore
+et dolore magna
+aliquyam erat, sed
+diam voluptua. At
+vero eos et
+accusam et justo
+duo dolores et ea
+rebum. Stet clita
+kasd gubergren,
+no sea takimata
+sanctus est Lorem
+ipsum dolor sit
+amet.
```
-*display "Hello World" with newlines between words*
+*Find the exact string '(Lorem|dolor)' in example.txt*
```bash
-echo -ne "Hello\nWorld\n"
+fgrep '(Lorem|dolor)' example.txt
+or
+grep -F '(Lorem|dolor)' example.txt
```
```bash
-Hello
-World
+foo (Lorem|dolor)
```
-### i. `fmt`
+### f. `fmt`
Simple optimal text formatter
*example: example.txt (1 line)*
@@ -480,35 +471,25 @@ ipsum dolor sit
amet.
```
-### j. `tr`
-Translate or delete characters
-
-*example.txt*
-```bash
-Hello World Foo Bar Baz!
-```
-
-*take all lower case letters and make them upper case*
-```bash
-cat example.txt | tr 'a-z' 'A-Z'
-```
+### g. `grep`
+Looks for text inside files. You can use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.
```bash
-HELLO WORLD FOO BAR BAZ!
+grep pattern filename
```
-
-*take all spaces and make them into newlines*
+Example:
```bash
-cat example.txt | tr ' ' '\n'
+$ grep admin /etc/passwd
+_kadmin_admin:*:218:-2:Kerberos Admin Service:/var/empty:/usr/bin/false
+_kadmin_changepw:*:219:-2:Kerberos Change Password Service:/var/empty:/usr/bin/false
+_krb_kadmin:*:231:-2:Open Directory Kerberos Admin Service:/var/empty:/usr/bin/false
```
+You can also force grep to ignore word case by using `-i` option. `-r` can be used to search all files under the specified directory, for example:
```bash
-Hello
-World
-Foo
-Bar
-Baz!
+$ grep -r admin /etc/
```
+And `-w` to search for words only. For more detail on `grep`, check following [link](https://www.cyberciti.biz/faq/grep-in-bash).
-### k. `nl`
+### h. `nl`
Number lines of files
*example.txt*
@@ -560,101 +541,160 @@ nl -s". " example.txt
19. amet.
```
-### l. `egrep`
-Print lines matching a pattern - Extended Expression (alias for: 'grep -E')
-
-*example.txt*
+### i. `sed`
+Stream editor for filtering and transforming text
+
+*example.txt*
+```bash
+Hello This is a Test 1 2 3 4
+```
+
+*replace all spaces with hyphens*
+```bash
+sed 's/ /-/g' example.txt
+```
+```bash
+Hello-This-is-a-Test-1-2-3-4
+```
+
+*replace all digits with "d"*
+```bash
+sed 's/[0-9]/d/g' example.txt
+```
+```bash
+Hello This is a Test d d d d
+```
+
+### j. `sort`
+Sort lines of text files
+
+*example.txt*
+```bash
+f
+b
+c
+g
+a
+e
+d
+```
+
+*sort example.txt*
+```bash
+sort example.txt
+```
+```bash
+a
+b
+c
+d
+e
+f
+g
+```
+
+*randomize a sorted example.txt*
+```bash
+sort example.txt | sort -R
+```
+```bash
+b
+f
+a
+c
+d
+g
+e
+```
+
+### k. `tr`
+Translate or delete characters
+
+*example.txt*
+```bash
+Hello World Foo Bar Baz!
+```
+
+*take all lower case letters and make them upper case*
```bash
-Lorem ipsum
-dolor sit amet,
-consetetur
-sadipscing elitr,
-sed diam nonumy
-eirmod tempor
-invidunt ut labore
-et dolore magna
-aliquyam erat, sed
-diam voluptua. At
-vero eos et
-accusam et justo
-duo dolores et ea
-rebum. Stet clita
-kasd gubergren,
-no sea takimata
-sanctus est Lorem
-ipsum dolor sit
-amet.
+cat example.txt | tr 'a-z' 'A-Z'
+```
+```bash
+HELLO WORLD FOO BAR BAZ!
```
-*display lines that have either "Lorem" or "dolor" in them.*
+*take all spaces and make them into newlines*
```bash
-egrep '(Lorem|dolor)' example.txt
-or
-grep -E '(Lorem|dolor)' example.txt
+cat example.txt | tr ' ' '\n'
```
```bash
-Lorem ipsum
-dolor sit amet,
-et dolore magna
-duo dolores et ea
-sanctus est Lorem
-ipsum dolor sit
+Hello
+World
+Foo
+Bar
+Baz!
```
-### m. `fgrep`
-Print lines matching a pattern - FIXED pattern matching (alias for: 'grep -F')
+### l. `uniq`
+Report or omit repeated lines
*example.txt*
```bash
-Lorem ipsum
-dolor sit amet,
-consetetur
-sadipscing elitr,
-sed diam nonumy
-eirmod tempor
-foo (Lorem|dolor)
-invidunt ut labore
-et dolore magna
-aliquyam erat, sed
-diam voluptua. At
-vero eos et
-accusam et justo
-duo dolores et ea
-rebum. Stet clita
-kasd gubergren,
-no sea takimata
-sanctus est Lorem
-ipsum dolor sit
-amet.
+a
+a
+b
+a
+b
+c
+d
+c
```
-*Find the exact string '(Lorem|doloar)' in example.txt*
+*show only unique lines of example.txt (first you need to sort it, otherwise it won't see the overlap)*
```bash
-fgrep '(Lorem|dolor)' example.txt
-or
-grep -F '(Lorem|dolor)' example.txt
+sort example.txt | uniq
```
```bash
-foo (Lorem|dolor)
+a
+b
+c
+d
+```
+
+*show the unique items for each line, and tell me how many instances it found*
+```bash
+sort example.txt | uniq -c
+```
+```bash
+ 3 a
+ 2 b
+ 2 c
+ 1 d
+```
+
+### m. `wc`
+Tells you how many lines, words and characters there are in a file.
+```bash
+wc filename
+```
+Example:
+```bash
+$ wc demo.txt
+7459 15915 398400 demo.txt
```
+Where `7459` is lines, `15915` is words and `398400` is characters.
## 1.3. Directory Operations
-### a. `mkdir`
-Makes a new directory.
-```bash
-mkdir dirname
-```
-
-### b. `cd`
+### a. `cd`
Moves you from one directory to other. Running this
```bash
$ cd
@@ -664,6 +704,21 @@ moves you to home directory. This command accepts an optional `dirname`, which m
cd dirname
```
+### b. `mkdir`
+Makes a new directory.
+```bash
+mkdir dirname
+```
+You can use this to create multiple directories at once within your current directory.
+```bash
+mkdir 1stDirectory 2ndDirectory 3rdDirectory
+```
+You can also use this to create parent directories at the same time. For instance, if you wanted a directory named 'project1' in another subdirectory at '/samples/bash/projects/', you could run:
+```bash
+mkdir /samples/bash/projects/project1
+```
+If any of these directories did no already exist, they would be created as well.
+
### c. `pwd`
Tells you which directory you currently are in.
```bash
@@ -674,82 +729,85 @@ pwd
-### a. `ssh`
-ssh (SSH client) is a program for logging into and executing commands on a remote machine.
-```bash
-ssh user@host
-```
-This command also accepts an option `-p` that can to used to connect to specific port.
-```bash
-ssh -p port user@host
-```
+### a. `bg`
+Lists stopped or background jobs; resume a stopped job in the background.
-### b. `whoami`
-Return current logged in username.
+### b. `cal`
+Shows the month's calendar.
-### c. `passwd`
-Allows the current logged user to change his password.
+### c. `date`
+Shows the current date and time.
-### d. `quota`
-Shows what your disk quota is.
+### d. `df`
+Shows disk usage.
+
+### e. `dig`
+Gets DNS information for domain.
```bash
-quota -v
+dig domain
```
-### e. `date`
-Shows the current date and time.
-
-### f. `cal`
-Shows the month's calendar.
+### f. `du`
+Shows the disk usage of files or directories. For more information on this command check this [link](http://www.linfo.org/du.html)
+```bash
+du [option] [filename|directory]
+```
+Options:
+- `-h` (human readable) Displays output it in kilobytes (K), megabytes (M) and gigabytes (G).
+- `-s` (supress or summarize) Outputs total disk space of a directory and supresses reports for subdirectories.
-### g. `uptime`
-Shows current uptime.
+Example:
+```bash
+du -sh pictures
+1.4M pictures
+```
-### h. `w`
-Displays who is online.
+### g. `fg`
+Brings the most recent job in the foreground.
-### i. `finger`
+### h. `finger`
Displays information about user.
```bash
finger username
```
+### i. `jobs`
+Lists the jobs running in the background, giving the job number.
-### j. `uname`
-Shows kernel information.
+### j. `last`
+Lists your last logins of specified user.
```bash
-uname -a
+last yourUsername
```
### k. `man`
@@ -758,98 +816,134 @@ Shows the manual for specified command.
man command
```
-### l. `df`
-Shows disk usage.
+### l. `passwd`
+Allows the current logged user to change their password.
-### m. `du`
-Shows the disk usage of the files and directories in filename (du -s give only a total).
+### m. `ping`
+Pings host and outputs results.
```bash
-du filename
+ping host
```
-### n. `last`
-Lists your last logins of specified user.
+### n. `ps`
+Lists your processes.
```bash
-last yourUsername
+ps -u yourusername
+```
+Use the flags ef. e for every process and f for full listing.
+```bash
+ps -ef
```
-### o. `ps`
-Lists your processes.
+### o. `quota`
+Shows what your disk quota is.
```bash
-ps -u yourusername
+quota -v
```
-### p. `kill`
-Kills (ends) the processes with the ID you gave.
+### p. `scp`
+Transfer files between a local host and a remote host or between two remote hosts.
+
+*copy from local host to remote host*
```bash
-kill PID
+scp source_file user@host:directory/target_file
+```
+*copy from remote host to local host*
+```bash
+scp user@host:directory/source_file target_file
+scp -r user@host:directory/source_folder target_folder
+```
+This command also accepts an option `-P` that can be used to connect to specific port.
+```bash
+scp -P port user@host:directory/source_file target_file
```
-### q. `killall`
-Kill all processes with the name.
+### q. `ssh`
+ssh (SSH client) is a program for logging into and executing commands on a remote machine.
```bash
-killall processname
+ssh user@host
+```
+This command also accepts an option `-p` that can be used to connect to specific port.
+```bash
+ssh -p port user@host
```
### r. `top`
Displays your currently active processes.
-### s. `bg`
-Lists stopped or background jobs ; resume a stopped job in the background.
+### s. `uname`
+Shows kernel information.
+```bash
+uname -a
+```
+
+### t. `uptime`
+Shows current uptime.
-### t. `fg`
-Brings the most recent job in the foreground.
+### u. `w`
+Displays who is online.
-### u. `ping`
-Pings host and outputs results.
+### v. `wget`
+Downloads file.
```bash
-ping host
+wget file
```
-### v. `whois`
+### w. `whoami`
+Return current logged in username.
+
+### x. `whois`
Gets whois information for domain.
```bash
whois domain
```
-### w. `dig`
-Gets DNS information for domain.
+## 1.5. Process Monitoring Operations
+
+
+
+### a. `kill`
+Kills (ends) the processes with the ID you gave.
```bash
-dig domain
+kill PID
```
-### x. `wget`
-Downloads file.
+### b. `killall`
+Kill all processes with the name.
```bash
-wget file
+killall processname
```
-
-### y. `scp`
-Transfer files between a local host and a remote host or between two remote hosts.
-
-*copy from local host to remote host*
+### c. &
+The `&` symbol instructs the command to run as a background process in a subshell.
```bash
-scp source_file user@host:directory/target_file
+command &
```
-*copy from remote host to local host*
+
+### d. `nohup`
+nohup stands for "No Hang Up". This allows to run command/process or shell script that can continue running in the background after you log out from a shell.
```bash
-scp user@host:directory/source_file target_file
-scp -r user@host:directory/source_folder farget_folder
+nohup command
```
-This command also accepts an option `-P` that can to used to connect to specific port.
+Combine it with `&` to create background processes
```bash
-scp -P port user@host:directory/source_file target_file
+nohup command &
```
-
# 2. Basic 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 an standalone executable without typing sh, bash, python, php etc beforehand in the terminal.
+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.
```bash
-#!/bin/bash
+#!/usr/bin/env bash
```
## 2.1. Variables
@@ -867,16 +961,16 @@ Example:
```bash
echo $str # hello world
```
-
-Like other languages bash has also arrays. An array is variable containing multiple values. There's no maximum limit on the size of array. Array 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.
+## 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.
Examples:
```bash
-array[0] = val
-array[1] = val
-array[2] = val
+array[0]=val
+array[1]=val
+array[2]=val
array=([2]=val [0]=val [1]=val)
-array(val val val)
+array=(val val val)
```
To display a value at specific index use following syntax:
@@ -899,7 +993,7 @@ ${varname:+word} # if varname exists and isn't null, return word; otherwise r
${varname:offset:length} # performs substring expansion. It returns the substring of $varname starting at offset and up to length characters
```
-## 2.2 String Substitution
+## 2.3 String Substitution
Check some of the syntax on how to manipulate strings
@@ -913,11 +1007,11 @@ ${variable//pattern/string} # the longest match to pattern in variable is replac
${#varname} # returns the length of the value of the variable as a character string
```
-## 2.3. Functions
+## 2.4. Functions
As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion. Declaring a function is just a matter of writing function my_func { my_code }. Calling a function is just like calling another program, you just write its name.
```bash
-functname() {
+function name() {
shell commands
}
```
@@ -936,14 +1030,14 @@ function say {
say "hello world!"
```
-When you run the above example the `hello` function will output "world!". The above two functions `hello` and `say` are identical. The main difference is function `say`. This function, prints the first argument it receives. Arguments, within funtions, are treated in the same manner as arguments given to the script.
+When you run the above example the `hello` function will output "world!". The above two functions `hello` and `say` are identical. The main difference is function `say`. This function, prints the first argument it receives. Arguments, within functions, are treated in the same manner as arguments given to the script.
-## 2.4. Conditionals
+## 2.5. Conditionals
The conditional statement in bash is similar to other programming languages. Conditions have many form like the most basic form is `if` expression `then` statement where statement is only executed if expression is true.
```bash
-if [expression]; then
+if [ expression ]; then
will execute only if expression is true
else
will execute if expression is false
@@ -966,7 +1060,7 @@ Expression Examples:
```bash
statement1 && statement2 # both statements are true
-statement1 || statement2 # one of the statement is true
+statement1 || statement2 # at least one of the statements is true
str1=str2 # str1 matches str2
str1!=str2 # str1 does not match str2
@@ -980,8 +1074,8 @@ str1>str2 # str1 is greater than str2
-e file # file exists; same -a
-f file # file exists and is a regular file (i.e., not a directory or other special type of file)
-r file # you have read permission
--r file # file exists and is not empty
--w file # your have write permission
+-s file # file exists and is not empty
+-w file # you have write permission
-x file # you have execute permission on file, or directory search permission if it is a directory
-N file # file was modified since it was last read
-O file # you own file
@@ -998,7 +1092,7 @@ file1 -ot file2 # file1 is older than file2
-ne # not equal
```
-## 2.5. Loops
+## 2.6. Loops
There are three types of loops in bash. `for`, `while` and `until`.
@@ -1036,17 +1130,72 @@ done
# 3. Tricks
-## set an alias
-Open `bash_profile` by running following command `nano ~/.bash_profile`
-> alias dockerlogin='ssh www-data@adnan.local -p2222' # add your alias in .bash_profile
+## Set an alias
+
+Run `nano ~/.bash_profile` and add the following line:
-## to quickly go to a specific directory
-nano ~/.bashrc
-> export hotellogs="/workspace/hotel-api/storage/logs"
+```bash
+alias dockerlogin='ssh www-data@adnan.local -p2222' # add your alias in .bash_profile
+```
+
+## To quickly go to a specific directory
+
+Run `nano ~/.bashrc` and add the following line:
+
+```bash
+export hotellogs="/workspace/hotel-api/storage/logs"
+```
+
+Now you can use the saved path:
+```bash
source ~/.bashrc
-cd hotellogs
+cd $hotellogs
+```
+
+## Re-execute the previous command
+
+This goes back to the days before you could rely on keyboards to have an "up" arrow key, but can still be useful.
+To run the last command in your history
+```bash
+!!
+```
+A common error is to forget to use `sudo` to prefix a command requiring privileged execution. Instead of typing the whole command again, you can:
+```bash
+sudo !!
+```
+This would change a `mkdir somedir` into `sudo mkdir somedir`.
+## Exit traps
+
+Make your bash scripts more robust by reliably performing cleanup.
+
+```bash
+function finish {
+ # your cleanup here. e.g. kill any forked processes
+ jobs -p | xargs kill
+}
+trap finish EXIT
+```
+
+## Saving your environment variables
+
+When you do `export FOO = BAR`, your variable is only exported in this current shell and all its children, to persist in the future you can simply append in your `~/.bash_profile` file the command to export your variable
+```bash
+echo export FOO=BAR >> ~/.bash_profile
+```
+
+## Accessing your scripts
+
+You can easily access your scripts by creating a bin folder in your home with `mkdir ~/bin`, now all the scripts you put in this folder you can access in any directory.
+
+If you can not access, try append the code below in your `~/.bash_profile` file and after do `source ~/.bash_profile`.
+```bash
+# set PATH so it includes user's private bin if it exists
+if [ -d "$HOME/bin" ] ; then
+ PATH="$HOME/bin:$PATH"
+fi
+```
# 4. Debugging
You can easily debug the bash script by passing different options to `bash` command. For example `-n` will not run commands and check for syntax errors only. `-v` echo commands before running them. `-x` echo commands after command-line processing.
@@ -1059,10 +1208,15 @@ bash -x scriptname
## Contribution
-- Report issues
-- Open pull request with improvements
+- Report issues [How to](https://help.github.com/articles/creating-an-issue/)
+- Open pull request with improvements [How to](https://help.github.com/articles/about-pull-requests/)
- Spread the word
+## Translation
+- [Chinese | 简体中文](https://github.com/vuuihc/bash-guide)
+- [Turkish | Türkçe](https://github.com/omergulen/bash-guide)
+- [Japanese | 日本語](https://github.com/itooww/bash-guide)
+
## License
[![License: CC BY 4.0](https://img.shields.io/badge/License-CC%20BY%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by/4.0/)