Bash
Functions Info
apropos - List of man pages for possible matches based on a search term.
Commands
Man Page Organization
Redirecting Standard Output
.bashls -l /usr/bin > ls-output.txt- This makes output of ls command
About
uniquniqto remove any duplicates from the output of thesortcommand..bashls /bin /usr/bin | sort | uniqTo see the list of duplicates instead, add the
doption touniq.bashls /bin /usr/bin | sort | uniq -d
About
teeteeis likels > output.txtbut it can work with grep command..bashls /usr/bin | tee ls.txt | grep zip
About
echo.bashecho $((2 + 2))Symbols
echoArithmetic Exponentiation can be nested.bashecho $(($((5**2)) * 3))The range of integers.
.bashecho {1..10} echo a{A{1,2},B{3,4}}bEnverment Value List
.bashcli printenv | less set | less
History
To see the history
.bashhistoryHistory Command
History Expansion Commands
To record the command
.bashscript
Permissions
To see the group number
.bashidFile Type
Permission Attributes -rwxrwxr–
File Modes in Binary and Octal
The attribute for changing the file permission
7 (rwx),6 (rw-),5 (r-x),4 (r--), and0 (---)
chmodSymbolic Notationumaskcommand controls the default permissions given to a file when it is created..bashumask 005
File System
Testing and Repairing File Systems
.bashsudo fsck /dev/sdb1Make bootable USB drive
.bashsudo dd bs=4M if=path/to/input.iso of=/dev/sd<?> conv=fdatasync status=progress
Helpful Command
Execute
findfile with command usexargs.bashfind ~ -type f -name "index.html" | xargs ls -lUse
findto find the file.bashfind ~ -type f -name "index.html"Best way to compress in tar
.bashtar cfJ playground.tar.xz playgroundHow to Mount ISO File on Linux
.bashmount -t iso9660 -o loop image.iso /mnt/iso_imageHow to sort in Linux
.bashsort -nk 5 # With command ls -l /usr/share | sort -nk 5 # Sort in ':' sort -ht ':' -k 3 /etc/passwd | headRemove Duplicate use
uniq.bashsort foo.txt | uniqSpell check in Terminal
.bashaspell check filename # To check HTML code aspell -H check html-filenameTo make PDF
.bashls -l ~/ | pr pr.ps | groff > pr.ps # Convert PostScript file into Potable Document File ps2pdf pr.ps pr.pdfGood Locations for Scripts
~/binScripts intended for personal use./usr/local/binScript that everyone on a system is allowed to use./usr/local/sbinScripts intended for use by the system administrator/usr/localLocally supplied software, scripts and compiled programs
Shell Script
Assigning Values to Variables and Constants.
.basha=z # Assign the string "z" to variable a. b="a string" # Embedded spaces must be within quotes. c="a string and $b" # Other expansions such as variables can be expanded into the assignment. d="$(ls -l foo.txt)" # Results of a command. e=$((5 * 7)) # Arithmetic expansion. f="\t\ta string\n" # Escape sequences such as tabs and newlines.Using variables with other commands.
.bashfilename="myFile" touch file mv file ${filename}Shell Functions
.bash# !/bin/bash z=human # Gobal variable echo_1() { echo "hello" return } echo_2() { i=world # local veariable echo "world" return } echo ${echo_1} ${echo_2} $zFlow Control: Branching with if
if.bashx=5 if [ "$x" -eq 5 ]; then echo "x equals 5." else echo "x does not equal 5." fiifin shell.bashif [ “$x” -eq 5 ]; then echo "equals 5"; else echo "does not equal 5"; fiif,elifandelse.bashif commands; then commands [elif commands; then commands...] [else commands] fi
Expressions Examples
File Expressions
.bash#!/bin/bash # test-file: Evaluate the status of a file FILE=~/.bashrc if [ -e "$FILE" ]; then if [ -f "$FILE" ]; then echo "$FILE is a regular file." fi if [ -d "$FILE" ]; then echo "$FILE is a directory." fi if [ -r "$FILE" ]; then echo "$FILE is readable." fi if [ -w "$FILE" ]; then echo "$FILE is writable." fi if [ -x "$FILE" ]; then echo "$FILE is executable/searchable." fi else echo "$FILE does not exist" exit 1 fi exitString Expressions
.bash#!/bin/bash # test-string: evaluate the value of a string ANSWER=maybe if [ -z "$ANSWER" ]; then echo "There is no answer." >&2 exit 1 fi if [ "$ANSWER" = "yes" ]; then echo "The answer is YES." elif [ "$ANSWER" = "no" ]; then echo "The answer is NO." elif [ "$ANSWER" = "maybe" ]; then echo "The answer is MAYBE." else echo "The answer is UNKNOWN." fiInteger Expressions
.bash#!/bin/bash # test-integer: evaluate the value of an integer. INT=-5 if [ -z "$INT" ]; then echo "INT is empty." >&2 exit 1 fi if [ "$INT" -eq 0 ]; then echo "INT is zero." else if [ "$INT" -lt 0 ]; then echo "INT is negative." else echo "INT is positive." fi if [ $((INT % 2)) -eq 0 ]; then echo "INT is even." else echo "INT is odd." fi fiFile Expressions
| Expression | Is True If: |
| --- | --- |
| file1 -ef file2 | file1 and file2 have the same inode numbers (the two filenames refer to the same file by hard linking). |
| file1 -nt file2 | file1 is newer than file2. |
| file1 -ot file2 | file1 is older than file2. |
| -b file | file exists and is a block-special (device) file. |
| -c file | file exists and is a character-special (device) file. |
| -d file | file exists and is a directory. |
| -e file | file exists. |
| -f file | file exists and is a regular file. |
| -g file | file exists and is set-group-ID. |
| -G file | file exists and is owned by the effective group ID. |
| -k file | file exists and has its “sticky bit” set. |
| -L file | file exists and is a symbolic link. |
| -O file | file exists and is owned by the effective user ID. |
| -p file | file exists and is a named pipe. |
| -r file | file exists and is readable (has readable permission for the effective user). |
| -s file | file exists and has a length greater than zero. |
| -S file | file exists and is a network socket. |
| -t fd | fd is a file descriptor directed to/from the terminal. This can be used to determine whether standard input/output/error is being redirected. |
| -u file | file exists and is setuid. |
| -w file | file exists and is writable (has write permission for the effective user). |
| -x file | file exists and is executable (has execute/search permission for the effective user). |
- String Expressions
| Expression | Is True If… |
| --- | --- |
| string | string is not null. |
| -n string | The length of string is greater than zero. |
| -z string | The length of string is zero. |
| `string1 = string2` `string1 == string2` | string1 and string2 are equal. Single or double equal signs may be used. The use of double equal signs is supported by bash and is generally preferred, but it is not POSIX compliant. |
| string1 != string2 | string1 and string2 are not equal. |
| string1 > string2 | string1 sorts after string2. |
| string1 < string2 | string1 sorts before string2. |
- Integer Expressions
| Expression | Is True If… |
|---|---|
| integer1 -eq integer2 | integer1 is equal to integer2. |
| integer1 -ne integer2 | integer1 is not equal to integer2. |
| integer1 -le integer2 | integer1 is less than or equal to integer2. |
| integer1 is less than or equal to integer2. | integer1 is less than integer2. |
| integer1 -ge integer2 | integer1 is greater than or equal to integer2. |
| integer1 -gt integer2 | integer1 is greater than integer2. |