├── .github
└── FUNDING.yml
├── LICENSE.md
└── README.md
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [idnan]
4 | custom: https://paypal.me/id9a9
5 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Zeeshan Ahmad
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | ## Table of Contents
6 | 1. [Basic Operations](#1-basic-operations)
7 | 1.1. [File Operations](#11-file-operations)
8 | 1.2. [Text Operations](#12-text-operations)
9 | 1.3. [Directory Operations](#13-directory-operations)
10 | 1.4. [SSH, System Info & Network Operations](#14-ssh-system-info--network-operations)
11 | 1.5. [Process Monitoring Operations](#15-process-monitoring-operations)
12 | 2. [Basic Shell Programming](#2-basic-shell-programming)
13 | 2.1. [Variables](#21-variables)
14 | 2.2. [Array](#22-array)
15 | 2.3. [String Substitution](#23-string-substitution)
16 | 2.4. [Other String Tricks](#24-other-string-tricks)
17 | 2.5. [Functions](#25-functions)
18 | 2.6. [Conditionals](#26-conditionals)
19 | 2.7. [Loops](#27-loops)
20 | 2.8. [Regex](#28-regex)
21 | 2.9. [Pipes](#29-pipes)
22 | 3. [Tricks](#3-tricks)
23 | 4. [Debugging](#4-debugging)
24 | 5. [Multi-threading](#5-multi-threading)
25 |
26 | # 1. Basic Operations
27 |
28 | ### a. `export`
29 | Displays all environment variables. If you want to get details of a specific variable, use `echo $VARIABLE_NAME`.
30 | ```bash
31 | export
32 | ```
33 | Example:
34 | ```bash
35 | $ export
36 | AWS_HOME=/Users/adnanadnan/.aws
37 | LANG=en_US.UTF-8
38 | LC_CTYPE=en_US.UTF-8
39 | LESS=-R
40 |
41 | $ echo $AWS_HOME
42 | /Users/adnanadnan/.aws
43 | ```
44 |
45 | ### b. `whatis`
46 | whatis shows description for user commands, system calls, library functions, and others in manual pages
47 | ```bash
48 | whatis something
49 | ```
50 | Example:
51 | ```bash
52 | $ whatis bash
53 | bash (1) - GNU Bourne-Again SHell
54 | ```
55 |
56 | ### c. `whereis`
57 | whereis searches for executables, source files, and manual pages using a database built by system automatically.
58 | ```bash
59 | whereis name
60 | ```
61 | Example:
62 | ```bash
63 | $ whereis php
64 | /usr/bin/php
65 | ```
66 |
67 | ### d. `which`
68 | which searches for executables in the directories specified by the environment variable PATH. This command will print the full path of the executable(s).
69 | ```bash
70 | which program_name
71 | ```
72 | Example:
73 | ```bash
74 | $ which php
75 | /c/xampp/php/php
76 | ```
77 |
78 | ### e. clear
79 | Clears content on window.
80 |
81 | ## 1.1. File Operations
82 |
109 |
110 | ### a. `cat`
111 | It can be used for the following purposes under UNIX or Linux.
112 | * Display text files on screen
113 | * Copy text files
114 | * Combine text files
115 | * Create new text files
116 | ```bash
117 | cat filename
118 | cat file1 file2
119 | cat file1 file2 > newcombinedfile
120 | cat < file1 > file2 #copy file1 to file2
121 | ```
122 |
123 | ### b. `chmod`
124 | 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).
125 | ```bash
126 | chmod -options filename
127 | ```
128 |
129 | ### c. `chown`
130 | 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.
131 | ```bash
132 | chown -options user:group filename
133 | ```
134 |
135 | ### d. `cp`
136 | Copies a file from one location to other.
137 | ```bash
138 | cp filename1 filename2
139 | ```
140 | Where `filename1` is the source path to the file and `filename2` is the destination path to the file.
141 |
142 | ### e. `diff`
143 | Compares files, and lists their differences.
144 | ```bash
145 | diff filename1 filename2
146 | ```
147 |
148 | ### f. `file`
149 | Determine file type.
150 | ```bash
151 | file filename
152 | ```
153 | Example:
154 | ```bash
155 | $ file index.html
156 | index.html: HTML document, ASCII text
157 | ```
158 | ### g. `find`
159 | Find files in directory
160 | ```bash
161 | find directory options pattern
162 | ```
163 | Example:
164 | ```bash
165 | $ find . -name README.md
166 | $ find /home/user1 -name '*.png'
167 | ```
168 |
169 | ### h. `gunzip`
170 | Un-compresses files compressed by gzip.
171 | ```bash
172 | gunzip filename
173 | ```
174 |
175 | ### i. `gzcat`
176 | Lets you look at gzipped file without actually having to gunzip it.
177 | ```bash
178 | gzcat filename
179 | ```
180 |
181 | ### j. `gzip`
182 | Compresses files.
183 | ```bash
184 | gzip filename
185 | ```
186 |
187 | ### k. `head`
188 | Outputs the first 10 lines of file
189 | ```bash
190 | head filename
191 | ```
192 |
193 | ### l. `less`
194 | Shows the contents of a file or a command output, one page at a time. It is similar to [more](#q-more), but has more advanced features and allows you to navigate both forward and backward through the file.
195 | ```bash
196 | less filename
197 | ```
198 |
199 | ### m. `lpq`
200 | Check out the printer queue.
201 | ```bash
202 | lpq
203 | ```
204 | Example:
205 | ```bash
206 | $ lpq
207 | Rank Owner Job File(s) Total Size
208 | active adnanad 59 demo 399360 bytes
209 | 1st adnanad 60 (stdin) 0 bytes
210 | ```
211 |
212 | ### n. `lpr`
213 | Print the file.
214 | ```bash
215 | lpr filename
216 | ```
217 |
218 | ### o. `lprm`
219 | Remove something from the printer queue.
220 | ```bash
221 | lprm jobnumber
222 | ```
223 |
224 | ### p. `ls`
225 | 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).
226 | ```bash
227 | ls option
228 | ```
229 | Example:
230 |
231 | $ ls -la
232 | rwxr-xr-x 33 adnan staff 1122 Mar 27 18:44 .
233 | drwxrwxrwx 60 adnan staff 2040 Mar 21 15:06 ..
234 | -rw-r--r--@ 1 adnan staff 14340 Mar 23 15:05 .DS_Store
235 | -rw-r--r-- 1 adnan staff 157 Mar 25 18:08 .bumpversion.cfg
236 | -rw-r--r-- 1 adnan staff 6515 Mar 25 18:08 .config.ini
237 | -rw-r--r-- 1 adnan staff 5805 Mar 27 18:44 .config.override.ini
238 | drwxr-xr-x 17 adnan staff 578 Mar 27 23:36 .git
239 | -rwxr-xr-x 1 adnan staff 2702 Mar 25 18:08 .gitignore
240 |
241 |
242 | ### q. `more`
243 | Shows the first part of a file (move with space and type q to quit).
244 | ```bash
245 | more filename
246 | ```
247 |
248 | ### r. `mv`
249 | Moves a file from one location to other.
250 | ```bash
251 | mv filename1 filename2
252 | ```
253 | Where `filename1` is the source path to the file and `filename2` is the destination path to the file.
254 |
255 | Also it can be used for rename a file.
256 | ```bash
257 | mv old_name new_name
258 | ```
259 |
260 | ### s. `rm`
261 | Removes a file. Using this command on a directory gives you an error.
262 | `rm: directory: is a directory`
263 | 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.
264 | ```bash
265 | rm filename
266 | ```
267 |
268 | ### t. `tail`
269 | Outputs the last 10 lines of file. Use `-f` to output appended data as the file grows.
270 | ```bash
271 | tail filename
272 | ```
273 |
274 | ### u. `touch`
275 | Updates access and modification time stamps of your file. If it doesn't exists, it'll be created.
276 | ```bash
277 | touch filename
278 | ```
279 | Example:
280 | ```bash
281 | $ touch trick.md
282 | ```
283 |
284 | ## 1.2. Text Operations
285 |
286 |
305 |
306 | ### a. `awk`
307 | awk is the most useful command for handling text files. It operates on an entire file line by line. By default it uses whitespace to separate the fields. The most common syntax for awk command is
308 |
309 | ```bash
310 | awk '/search_pattern/ { action_to_take_if_pattern_matches; }' file_to_parse
311 | ```
312 |
313 | Lets take following file `/etc/passwd`. Here's the sample data that this file contains:
314 | ```
315 | root:x:0:0:root:/root:/usr/bin/zsh
316 | daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
317 | bin:x:2:2:bin:/bin:/usr/sbin/nologin
318 | sys:x:3:3:sys:/dev:/usr/sbin/nologin
319 | sync:x:4:65534:sync:/bin:/bin/sync
320 | ```
321 | So now lets get only username from this file. Where `-F` specifies that on which base we are going to separate the fields. In our case it's `:`. `{ print $1 }` means print out the first matching field.
322 | ```bash
323 | awk -F':' '{ print $1 }' /etc/passwd
324 | ```
325 | After running the above command you will get following output.
326 | ```
327 | root
328 | daemon
329 | bin
330 | sys
331 | sync
332 | ```
333 | For more detail on how to use `awk`, check following [link](https://www.cyberciti.biz/faq/bash-scripting-using-awk).
334 |
335 |
336 | ### b. `cut`
337 | Remove sections from each line of files
338 |
339 | *example.txt*
340 | ```bash
341 | red riding hood went to the park to play
342 | ```
343 |
344 | *show me columns 2 , 7 , and 9 with a space as a separator*
345 | ```bash
346 | cut -d " " -f2,7,9 example.txt
347 | ```
348 | ```bash
349 | riding park play
350 | ```
351 |
352 | ### c. `echo`
353 | Display a line of text
354 |
355 | *display "Hello World"*
356 | ```bash
357 | echo Hello World
358 | ```
359 | ```bash
360 | Hello World
361 | ```
362 |
363 | *display "Hello World" with newlines between words*
364 | ```bash
365 | echo -ne "Hello\nWorld\n"
366 | ```
367 | ```bash
368 | Hello
369 | World
370 | ```
371 |
372 | ### d. `egrep`
373 | Print lines matching a pattern - Extended Expression (alias for: 'grep -E')
374 |
375 | *example.txt*
376 | ```bash
377 | Lorem ipsum
378 | dolor sit amet,
379 | consetetur
380 | sadipscing elitr,
381 | sed diam nonumy
382 | eirmod tempor
383 | invidunt ut labore
384 | et dolore magna
385 | aliquyam erat, sed
386 | diam voluptua. At
387 | vero eos et
388 | accusam et justo
389 | duo dolores et ea
390 | rebum. Stet clita
391 | kasd gubergren,
392 | no sea takimata
393 | sanctus est Lorem
394 | ipsum dolor sit
395 | amet.
396 | ```
397 |
398 | *display lines that have either "Lorem" or "dolor" in them.*
399 | ```bash
400 | egrep '(Lorem|dolor)' example.txt
401 | or
402 | grep -E '(Lorem|dolor)' example.txt
403 | ```
404 | ```bash
405 | Lorem ipsum
406 | dolor sit amet,
407 | et dolore magna
408 | duo dolores et ea
409 | sanctus est Lorem
410 | ipsum dolor sit
411 | ```
412 |
413 | ### e. `fgrep`
414 | Print lines matching a pattern - FIXED pattern matching (alias for: 'grep -F')
415 |
416 | *example.txt*
417 | ```bash
418 | Lorem ipsum
419 | dolor sit amet,
420 | consetetur
421 | sadipscing elitr,
422 | sed diam nonumy
423 | eirmod tempor
424 | foo (Lorem|dolor)
425 | invidunt ut labore
426 | et dolore magna
427 | aliquyam erat, sed
428 | diam voluptua. At
429 | vero eos et
430 | accusam et justo
431 | duo dolores et ea
432 | rebum. Stet clita
433 | kasd gubergren,
434 | no sea takimata
435 | sanctus est Lorem
436 | ipsum dolor sit
437 | amet.
438 | ```
439 |
440 | *Find the exact string '(Lorem|dolor)' in example.txt*
441 | ```bash
442 | fgrep '(Lorem|dolor)' example.txt
443 | or
444 | grep -F '(Lorem|dolor)' example.txt
445 | ```
446 | ```bash
447 | foo (Lorem|dolor)
448 | ```
449 |
450 | ### f. `fmt`
451 | Simple optimal text formatter
452 |
453 | *example: example.txt (1 line)*
454 | ```bash
455 | 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.
456 | ```
457 |
458 | *output the lines of example.txt to 20 character width*
459 | ```bash
460 | cat example.txt | fmt -w 20
461 | ```
462 | ```bash
463 | Lorem ipsum
464 | dolor sit amet,
465 | consetetur
466 | sadipscing elitr,
467 | sed diam nonumy
468 | eirmod tempor
469 | invidunt ut labore
470 | et dolore magna
471 | aliquyam erat, sed
472 | diam voluptua. At
473 | vero eos et
474 | accusam et justo
475 | duo dolores et ea
476 | rebum. Stet clita
477 | kasd gubergren,
478 | no sea takimata
479 | sanctus est Lorem
480 | ipsum dolor sit
481 | amet.
482 | ```
483 |
484 | ### g. `grep`
485 | 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.
486 | ```bash
487 | grep pattern filename
488 | ```
489 | Example:
490 | ```bash
491 | $ grep admin /etc/passwd
492 | _kadmin_admin:*:218:-2:Kerberos Admin Service:/var/empty:/usr/bin/false
493 | _kadmin_changepw:*:219:-2:Kerberos Change Password Service:/var/empty:/usr/bin/false
494 | _krb_kadmin:*:231:-2:Open Directory Kerberos Admin Service:/var/empty:/usr/bin/false
495 | ```
496 | 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:
497 | ```bash
498 | $ grep -r admin /etc/
499 | ```
500 | And `-w` to search for words only. For more detail on `grep`, check following [link](https://www.cyberciti.biz/faq/grep-in-bash).
501 |
502 | ### h. `nl`
503 | Number lines of files
504 |
505 | *example.txt*
506 | ```bash
507 | Lorem ipsum
508 | dolor sit amet,
509 | consetetur
510 | sadipscing elitr,
511 | sed diam nonumy
512 | eirmod tempor
513 | invidunt ut labore
514 | et dolore magna
515 | aliquyam erat, sed
516 | diam voluptua. At
517 | vero eos et
518 | accusam et justo
519 | duo dolores et ea
520 | rebum. Stet clita
521 | kasd gubergren,
522 | no sea takimata
523 | sanctus est Lorem
524 | ipsum dolor sit
525 | amet.
526 | ```
527 |
528 | *show example.txt with line numbers*
529 | ```bash
530 | nl -s". " example.txt
531 | ```
532 | ```bash
533 | 1. Lorem ipsum
534 | 2. dolor sit amet,
535 | 3. consetetur
536 | 4. sadipscing elitr,
537 | 5. sed diam nonumy
538 | 6. eirmod tempor
539 | 7. invidunt ut labore
540 | 8. et dolore magna
541 | 9. aliquyam erat, sed
542 | 10. diam voluptua. At
543 | 11. vero eos et
544 | 12. accusam et justo
545 | 13. duo dolores et ea
546 | 14. rebum. Stet clita
547 | 15. kasd gubergren,
548 | 16. no sea takimata
549 | 17. sanctus est Lorem
550 | 18. ipsum dolor sit
551 | 19. amet.
552 | ```
553 |
554 | ### i. `sed`
555 | Stream editor for filtering and transforming text
556 |
557 | *example.txt*
558 | ```bash
559 | Hello This is a Test 1 2 3 4
560 | ```
561 |
562 | *replace all spaces with hyphens*
563 | ```bash
564 | sed 's/ /-/g' example.txt
565 | ```
566 | ```bash
567 | Hello-This-is-a-Test-1-2-3-4
568 | ```
569 |
570 | *replace all digits with "d"*
571 | ```bash
572 | sed 's/[0-9]/d/g' example.txt
573 | ```
574 | ```bash
575 | Hello This is a Test d d d d
576 | ```
577 |
578 | ### j. `sort`
579 | Sort lines of text files
580 |
581 | *example.txt*
582 | ```bash
583 | f
584 | b
585 | c
586 | g
587 | a
588 | e
589 | d
590 | ```
591 |
592 | *sort example.txt*
593 | ```bash
594 | sort example.txt
595 | ```
596 | ```bash
597 | a
598 | b
599 | c
600 | d
601 | e
602 | f
603 | g
604 | ```
605 |
606 | *randomize a sorted example.txt*
607 | ```bash
608 | sort example.txt | sort -R
609 | ```
610 | ```bash
611 | b
612 | f
613 | a
614 | c
615 | d
616 | g
617 | e
618 | ```
619 |
620 | ### k. `tr`
621 | Translate or delete characters
622 |
623 | *example.txt*
624 | ```bash
625 | Hello World Foo Bar Baz!
626 | ```
627 |
628 | *take all lower case letters and make them upper case*
629 | ```bash
630 | cat example.txt | tr 'a-z' 'A-Z'
631 | ```
632 | ```bash
633 | HELLO WORLD FOO BAR BAZ!
634 | ```
635 |
636 | *take all spaces and make them into newlines*
637 | ```bash
638 | cat example.txt | tr ' ' '\n'
639 | ```
640 | ```bash
641 | Hello
642 | World
643 | Foo
644 | Bar
645 | Baz!
646 | ```
647 |
648 | ### l. `uniq`
649 | Report or omit repeated lines
650 |
651 | *example.txt*
652 | ```bash
653 | a
654 | a
655 | b
656 | a
657 | b
658 | c
659 | d
660 | c
661 | ```
662 |
663 | *show only unique lines of example.txt (first you need to sort it, otherwise it won't see the overlap)*
664 | ```bash
665 | sort example.txt | uniq
666 | ```
667 | ```bash
668 | a
669 | b
670 | c
671 | d
672 | ```
673 |
674 | *show the unique items for each line, and tell me how many instances it found*
675 | ```bash
676 | sort example.txt | uniq -c
677 | ```
678 | ```bash
679 | 3 a
680 | 2 b
681 | 2 c
682 | 1 d
683 | ```
684 |
685 | ### m. `wc`
686 | Tells you how many lines, words and characters there are in a file.
687 | ```bash
688 | wc filename
689 | ```
690 | Example:
691 | ```bash
692 | $ wc demo.txt
693 | 7459 15915 398400 demo.txt
694 | ```
695 | Where `7459` is lines, `15915` is words and `398400` is characters.
696 |
697 | ## 1.3. Directory Operations
698 |
699 |
706 |
707 | ### a. `cd`
708 | Moves you from one directory to other. Running this
709 | ```bash
710 | $ cd
711 | ```
712 | moves you to home directory. This command accepts an optional `dirname`, which moves you to that directory.
713 | ```bash
714 | cd dirname
715 | ```
716 | Switch to the previous working directory
717 | ```bash
718 | cd -
719 | ```
720 |
721 | ### b. `mkdir`
722 | Makes a new directory.
723 | ```bash
724 | mkdir dirname
725 | ```
726 | You can use this to create multiple directories at once within your current directory.
727 | ```bash
728 | mkdir 1stDirectory 2ndDirectory 3rdDirectory
729 | ```
730 | You can also use this to create parent directories at the same time with the -p (or --parents) flag. For instance, if you wanted a directory named 'project1' in another subdirectory at '/samples/bash/projects/', you could run:
731 | ```bash
732 | mkdir -p /samples/bash/projects/project1
733 | mkdir --parents /samples/bash/projects/project1
734 | ```
735 | Both commands above will do the same thing.
736 | If any of these directories did no already exist, they would be created as well.
737 |
738 | ### c. `pwd`
739 | Tells you which directory you currently are in.
740 | ```bash
741 | pwd
742 | ```
743 |
744 | ## 1.4. SSH, System Info & Network Operations
745 |
746 |
780 |
781 | ### a. `bg`
782 | Lists stopped or background jobs; resume a stopped job in the background.
783 |
784 | ### b. `cal`
785 | Shows the month's calendar.
786 |
787 | ### c. `date`
788 | Shows the current date and time.
789 |
790 | ### d. `df`
791 | Shows disk usage.
792 |
793 | ### e. `dig`
794 | Gets DNS information for domain.
795 | ```bash
796 | dig domain
797 | ```
798 |
799 | ### f. `du`
800 | Shows the disk usage of files or directories. For more information on this command check this [link](http://www.linfo.org/du.html)
801 | ```bash
802 | du [option] [filename|directory]
803 | ```
804 | Options:
805 | - `-h` (human readable) Displays output it in kilobytes (K), megabytes (M) and gigabytes (G).
806 | - `-s` (supress or summarize) Outputs total disk space of a directory and supresses reports for subdirectories.
807 |
808 | Example:
809 | ```bash
810 | du -sh pictures
811 | 1.4M pictures
812 | ```
813 |
814 | ### g. `fg`
815 | Brings the most recent job in the foreground.
816 |
817 | ### h. `finger`
818 | Displays information about user.
819 | ```bash
820 | finger username
821 | ```
822 | ### i. `jobs`
823 | Lists the jobs running in the background, giving the job number.
824 |
825 | ### j. `last`
826 | Lists your last logins of specified user.
827 | ```bash
828 | last yourUsername
829 | ```
830 |
831 | ### k. `man`
832 | Shows the manual for specified command.
833 | ```bash
834 | man command
835 | ```
836 |
837 | ### l. `passwd`
838 | Allows the current logged user to change their password.
839 |
840 | ### m. `ping`
841 | Pings host and outputs results.
842 | ```bash
843 | ping host
844 | ```
845 |
846 | ### n. `ps`
847 | Lists your processes.
848 | ```bash
849 | ps -u yourusername
850 | ```
851 | Use the flags ef. e for every process and f for full listing.
852 | ```bash
853 | ps -ef
854 | ```
855 |
856 | ### o. `quota`
857 | Shows what your disk quota is.
858 | ```bash
859 | quota -v
860 | ```
861 |
862 | ### p. `scp`
863 | Transfer files between a local host and a remote host or between two remote hosts.
864 |
865 | *copy from local host to remote host*
866 | ```bash
867 | scp source_file user@host:directory/target_file
868 | ```
869 | *copy from remote host to local host*
870 | ```bash
871 | scp user@host:directory/source_file target_file
872 | scp -r user@host:directory/source_folder target_folder
873 | ```
874 | This command also accepts an option `-P` that can be used to connect to specific port.
875 | ```bash
876 | scp -P port user@host:directory/source_file target_file
877 | ```
878 |
879 | ### q. `ssh`
880 | ssh (SSH client) is a program for logging into and executing commands on a remote machine.
881 | ```bash
882 | ssh user@host
883 | ```
884 | This command also accepts an option `-p` that can be used to connect to specific port.
885 | ```bash
886 | ssh -p port user@host
887 | ```
888 |
889 | ### r. `top`
890 | Displays your currently active processes.
891 |
892 | ### s. `uname`
893 | Shows kernel information.
894 | ```bash
895 | uname -a
896 | ```
897 |
898 | ### t. `uptime`
899 | Shows current uptime.
900 |
901 | ### u. `w`
902 | Displays who is online.
903 |
904 | ### v. `wget`
905 | Downloads file.
906 | ```bash
907 | wget file
908 | ```
909 |
910 | ### w. `whoami`
911 | Return current logged in username.
912 |
913 | ### x. `whois`
914 | Gets whois information for domain.
915 | ```bash
916 | whois domain
917 | ```
918 |
919 | ### y. `rsync`
920 | Does the same job as `scp` command, but transfers only changed files. Useful when transferring the same folder to/from server multiple times.
921 | ```bash
922 | rsync source_folder user@host:target_folder
923 | rsync user@host:target_folder target_folder
924 | ```
925 |
926 | ### z. `curl`
927 | Curl is a command-line tool for requesting or sending data using URL syntax. Usefull on systems where you only have terminal available for making various requests.
928 | ```bash
929 | curl url
930 | ```
931 | Use `-X` or `--request` to specify which method you would like invoke (GET, POST, DELETE, ...).
932 | Use `-d ` or `--data ` to POST data on given URL.
933 |
934 | ## 1.5. Process Monitoring Operations
935 |
936 |
944 |
945 | ### a. `kill`
946 | Kills (ends) the processes with the ID you gave.
947 | ```bash
948 | kill PID
949 | ```
950 |
951 | ### b. `killall`
952 | Kill all processes with the name.
953 | ```bash
954 | killall processname
955 | ```
956 |
957 | ### c. &
958 | The `&` symbol instructs the command to run as a background process in a subshell.
959 | ```bash
960 | command &
961 | ```
962 |
963 | ### d. `nohup`
964 | 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.
965 | ```bash
966 | nohup command
967 | ```
968 | Combine it with `&` to create background processes
969 | ```bash
970 | nohup command &
971 | ```
972 |
973 | # 2. Basic Shell Programming
974 |
975 |
976 | 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.
977 |
978 | ```bash
979 | #!/usr/bin/env bash
980 | ```
981 |
982 | ## 2.1. Variables
983 |
984 | 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.
985 |
986 | Example:
987 | ```bash
988 | str="hello world"
989 | ```
990 |
991 | 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.
992 |
993 | Example:
994 | ```bash
995 | echo $str # hello world
996 | ```
997 | ## 2.2. Array
998 | 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.
999 |
1000 | Examples:
1001 | ```bash
1002 | array[0]=val
1003 | array[1]=val
1004 | array[2]=val
1005 | array=([2]=val [0]=val [1]=val)
1006 | array=(val val val)
1007 | ```
1008 | To display a value at specific index use following syntax:
1009 |
1010 | ```bash
1011 | ${array[i]} # where i is the index
1012 | ```
1013 |
1014 | If no index is supplied, array element 0 is assumed. To find out how many values there are in the array use the following syntax:
1015 |
1016 | ```bash
1017 | ${#array[@]}
1018 | ```
1019 |
1020 | Bash has also support for the ternary conditions. Check some examples below.
1021 |
1022 | ```bash
1023 | ${varname:-word} # if varname exists and isn't null, return its value; otherwise return word
1024 | ${varname:=word} # if varname exists and isn't null, return its value; otherwise set it word and then return its value
1025 | ${varname:+word} # if varname exists and isn't null, return word; otherwise return null
1026 | ${varname:offset:length} # performs substring expansion. It returns the substring of $varname starting at offset and up to length characters
1027 | ```
1028 |
1029 | ## 2.3 String Substitution
1030 |
1031 | Check some of the syntax on how to manipulate strings
1032 |
1033 | ```bash
1034 | ${variable#pattern} # if the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest
1035 | ${variable##pattern} # if the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest
1036 | ${variable%pattern} # if the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest
1037 | ${variable%%pattern} # if the pattern matches the end of the variable's value, delete the longest part that matches and return the rest
1038 | ${variable/pattern/string} # the longest match to pattern in variable is replaced by string. Only the first match is replaced
1039 | ${variable//pattern/string} # the longest match to pattern in variable is replaced by string. All matches are replaced
1040 | ${#varname} # returns the length of the value of the variable as a character string
1041 | ```
1042 |
1043 | ## 2.4. Other String Tricks
1044 |
1045 | Bash has multiple shorthand tricks for doing various things to strings.
1046 |
1047 | ```bash
1048 | ${variable,,} #this converts every letter in the variable to lowercase
1049 | ${variable^^} #this converts every letter in the variable to uppercase
1050 |
1051 | ${variable:2:8} #this returns a substring of a string, starting at the character at the 2 index(strings start at index 0, so this is the 3rd character),
1052 | #the substring will be 8 characters long, so this would return a string made of the 3rd to the 11th characters.
1053 | ```
1054 |
1055 | Here are some handy pattern matching tricks
1056 | ```bash
1057 | if [[ "$variable" == *subString* ]] #this returns true if the provided substring is in the variable
1058 | if [[ "$variable" != *subString* ]] #this returns true if the provided substring is not in the variable
1059 | if [[ "$variable" == subString* ]] #this returns true if the variable starts with the given subString
1060 | if [[ "$variable" == *subString ]] #this returns true if the variable ends with the given subString
1061 | ```
1062 |
1063 |
1064 | The above can be shortened using a case statement and the IN keyword
1065 | ```bash
1066 | case "$var" in
1067 | begin*)
1068 | #variable begins with "begin"
1069 | ;;
1070 | *subString*)
1071 | #subString is in variable
1072 | ;;
1073 |
1074 | *otherSubString*)
1075 | #otherSubString is in variable
1076 | ;;
1077 | esac
1078 | ```
1079 |
1080 | ## 2.5. Functions
1081 | 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.
1082 |
1083 | ```bash
1084 | function name() {
1085 | shell commands
1086 | }
1087 | ```
1088 |
1089 | Example:
1090 | ```bash
1091 | #!/bin/bash
1092 | function hello {
1093 | echo world!
1094 | }
1095 | hello
1096 |
1097 | function say {
1098 | echo $1
1099 | }
1100 | say "hello world!"
1101 | ```
1102 |
1103 | 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.
1104 |
1105 | ## 2.6. Conditionals
1106 |
1107 | 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.
1108 |
1109 | ```bash
1110 | if [ expression ]; then
1111 | will execute only if expression is true
1112 | else
1113 | will execute if expression is false
1114 | fi
1115 | ```
1116 |
1117 | Sometime if conditions becoming confusing so you can write the same condition using the `case statements`.
1118 |
1119 | ```bash
1120 | case expression in
1121 | pattern1 )
1122 | statements ;;
1123 | pattern2 )
1124 | statements ;;
1125 | ...
1126 | esac
1127 | ```
1128 |
1129 | Expression Examples:
1130 |
1131 | ```bash
1132 | statement1 && statement2 # both statements are true
1133 | statement1 || statement2 # at least one of the statements is true
1134 |
1135 | str1=str2 # str1 matches str2
1136 | str1!=str2 # str1 does not match str2
1137 | str1str2 # str1 is greater than str2
1139 | -n str1 # str1 is not null (has length greater than 0)
1140 | -z str1 # str1 is null (has length 0)
1141 |
1142 | -a file # file exists
1143 | -d file # file exists and is a directory
1144 | -e file # file exists; same -a
1145 | -f file # file exists and is a regular file (i.e., not a directory or other special type of file)
1146 | -r file # you have read permission
1147 | -s file # file exists and is not empty
1148 | -w file # you have write permission
1149 | -x file # you have execute permission on file, or directory search permission if it is a directory
1150 | -N file # file was modified since it was last read
1151 | -O file # you own file
1152 | -G file # file's group ID matches yours (or one of yours, if you are in multiple groups)
1153 |
1154 | file1 -nt file2 # file1 is newer than file2
1155 | file1 -ot file2 # file1 is older than file2
1156 |
1157 | -lt # less than
1158 | -le # less than or equal
1159 | -eq # equal
1160 | -ge # greater than or equal
1161 | -gt # greater than
1162 | -ne # not equal
1163 | ```
1164 |
1165 | ## 2.7. Loops
1166 |
1167 | There are three types of loops in bash. `for`, `while` and `until`.
1168 |
1169 | Different `for` Syntax:
1170 | ```bash
1171 | for name [in list]
1172 | do
1173 | statements that can use $name
1174 | done
1175 |
1176 | for (( initialisation ; ending condition ; update ))
1177 | do
1178 | statements...
1179 | done
1180 | ```
1181 |
1182 | `while` Syntax:
1183 | ```bash
1184 | while condition; do
1185 | statements
1186 | done
1187 | ```
1188 |
1189 | `until` Syntax:
1190 | ```bash
1191 | until condition; do
1192 | statements
1193 | done
1194 | ```
1195 |
1196 | # 2.8. Regex
1197 |
1198 | They are a powerful tool for manipulating and searching text. Here are some examples of regular expressions that use each `metacharacter`:
1199 |
1200 |
1214 |
1215 | ### a. `.` (dot)
1216 | Matches any single character except newline.
1217 | ```bash
1218 | grep h.t file.txt
1219 | ```
1220 | Output:
1221 | ```bash
1222 | hat
1223 | hot
1224 | hit
1225 | ```
1226 |
1227 | ### b. `*` (asterisk)
1228 | Matches zero or more occurrences of the preceding character or group.
1229 | ```bash
1230 | grep ab*c file.txt
1231 | ```
1232 | Output:
1233 | ```bash
1234 | ac
1235 | abc
1236 | abbc
1237 | abbbc
1238 | ```
1239 |
1240 | ### c. `+` (plus)
1241 | Matches one or more occurrences of the preceding character or group.
1242 | ```bash
1243 | grep ab+c file.txt
1244 | ```
1245 | Output:
1246 | ```bash
1247 | abc
1248 | abbc
1249 | abbbc
1250 | abbbbc
1251 | ```
1252 |
1253 | ### d. `?` (question mark)
1254 | Matches zero or one occurrence of the preceding character or group.
1255 | ```bash
1256 | grep ab?c file.txt
1257 | ```
1258 | Output:
1259 | ```bash
1260 | ac
1261 | abc
1262 | ```
1263 |
1264 | ### e. `|` (pipe)
1265 | Matches either the pattern to the left or the pattern to the right.
1266 | ```bash
1267 | egrep "cat|dog" file.txt
1268 | ```
1269 | Output:
1270 | ```bash
1271 | cat
1272 | dog
1273 | ```
1274 |
1275 | ### f. `[]` (character class)
1276 | Matches any character inside the brackets.
1277 | ```bash
1278 | [aeiou] will match any vowel
1279 | [a-z] will match any lowercase letter
1280 | ```
1281 |
1282 | ### g. `[]` (negated character class)
1283 | Matches any character not inside the brackets.
1284 | ```bash
1285 | [^aeiou] will match any consonant
1286 | [^a-z] will match any non-lowercase letter
1287 | ```
1288 |
1289 | ### h. `()` (grouping)
1290 | Groups multiple tokens together and creates a capture group.
1291 | ```bash
1292 | egrep "(ab)+" file.txt
1293 | ```
1294 |
1295 | Output:
1296 | ```bash
1297 | ab
1298 | abab
1299 | ababab
1300 | ```
1301 |
1302 | ### i. `{}` (quantifiers)
1303 | Matches a specific number of occurrences of the preceding character or group.
1304 | ```bash
1305 | egrep "a{3}" file.txt
1306 | ```
1307 |
1308 | Output:
1309 | ```bash
1310 | aaa
1311 | aaaa
1312 | aaaaa
1313 | ```
1314 |
1315 | ### j. `\` (escape)
1316 | Escapes the next character to match it literally.
1317 | ```bash
1318 | egrep "a\+" file.txt
1319 | ```
1320 |
1321 | Output:
1322 | ```bash
1323 | a+
1324 | ```
1325 | =======
1326 | ## 2.9. Pipes
1327 |
1328 | Multiple commands can be linked together with a pipe, `|`. A `|` will send the standard-output from command A to the standard-input of command B.
1329 | Pipes can also be constructed with the `|&` symbols. This will send the standard-output **and** standard-error from command A to the standard-input of command B.
1330 |
1331 | # 3. Tricks
1332 |
1333 | ## Set an alias
1334 |
1335 | Run `nano ~/.bash_profile` and add the following line:
1336 |
1337 | ```bash
1338 | alias dockerlogin='ssh www-data@adnan.local -p2222' # add your alias in .bash_profile
1339 | ```
1340 |
1341 | ## To quickly go to a specific directory
1342 |
1343 | Run `nano ~/.bashrc` and add the following line:
1344 |
1345 | ```bash
1346 | export hotellogs="/workspace/hotel-api/storage/logs"
1347 | ```
1348 |
1349 | Now you can use the saved path:
1350 |
1351 | ```bash
1352 | source ~/.bashrc
1353 | cd $hotellogs
1354 | ```
1355 |
1356 | ## Re-execute the previous command
1357 |
1358 | This goes back to the days before you could rely on keyboards to have an "up" arrow key, but can still be useful.
1359 | To run the last command in your history
1360 | ```bash
1361 | !!
1362 | ```
1363 | 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:
1364 | ```bash
1365 | sudo !!
1366 | ```
1367 | This would change a `mkdir somedir` into `sudo mkdir somedir`.
1368 |
1369 | ## Exit traps
1370 |
1371 | Make your bash scripts more robust by reliably performing cleanup.
1372 |
1373 | ```bash
1374 | function finish {
1375 | # your cleanup here. e.g. kill any forked processes
1376 | jobs -p | xargs kill
1377 | }
1378 | trap finish EXIT
1379 | ```
1380 |
1381 | ## Saving your environment variables
1382 |
1383 | 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
1384 | ```bash
1385 | echo export FOO=BAR >> ~/.bash_profile
1386 | ```
1387 |
1388 | ## Accessing your scripts
1389 |
1390 | 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.
1391 |
1392 | If you can not access, try append the code below in your `~/.bash_profile` file and after do `source ~/.bash_profile`.
1393 | ```bash
1394 | # set PATH so it includes user's private bin if it exists
1395 | if [ -d "$HOME/bin" ] ; then
1396 | PATH="$HOME/bin:$PATH"
1397 | fi
1398 | ```
1399 |
1400 | # 4. Debugging
1401 | 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.
1402 |
1403 | ```bash
1404 | bash -n scriptname
1405 | bash -v scriptname
1406 | bash -x scriptname
1407 | ```
1408 |
1409 | # 5. Multi-threading
1410 | You can easily multi-threading your jobs using `&`. All those jobs will then run in the background simultaneously and you can see the processes below are running using `jobs`.
1411 |
1412 | ```bash
1413 | sleep 15 & sleep 5 &
1414 | ```
1415 |
1416 | The optional `wait` command will then wait for all the jobs to finish.
1417 |
1418 | ```bash
1419 | sleep 10 & sleep 5 &
1420 | wait
1421 | ```
1422 |
1423 | ## Contribution
1424 |
1425 | - Report issues [How to](https://help.github.com/articles/creating-an-issue/)
1426 | - Open pull request with improvements [How to](https://help.github.com/articles/about-pull-requests/)
1427 | - Spread the word
1428 |
1429 | ## Translation
1430 | - [Chinese | 简体中文](https://github.com/vuuihc/bash-guide)
1431 | - [Turkish | Türkçe](https://github.com/omergulen/bash-guide)
1432 | - [Japanese | 日本語](https://github.com/itooww/bash-guide)
1433 | - [Russian | Русский](https://github.com/navinweb/bash-guide)
1434 | - [Vietnamese | Tiếng Việt](https://github.com/nguyenvanhieuvn/hoc-bash)
1435 | - [Spanish | Español](https://github.com/mariotristan/bash-guide)
1436 |
1437 | ## License
1438 |
1439 | [](https://creativecommons.org/licenses/by/4.0/)
1440 |
--------------------------------------------------------------------------------