# 1. Basic Operations

Displays all environment variables. If you want to get details of a specific variable, use echo $VARIABLE_NAME. Example: ### b. whatis whatis shows description for user commands, system calls, library functions, and others in manual pages Example: ### c. whereis whereis searches for executables, source files, and manual pages using a database built by system automatically. Example: ### 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). Example: ### e. clear Clears content on window. ## 1.1. File Operations  cat chmod chown cp diff file find gunzip gzcat gzip head lpq lpr lprm ls more mv rm tail touch ### a. cat It can be used for the following purposes under UNIX or Linux. • Display text files on screen • Copy text files • Combine text files • Create new text files ### 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. ### 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. ### d. cp Copies a file from one location to other. 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. ### f. file Determine file type. Example: ### g. find Find files in directory Example: ### h. gunzip Un-compresses files compressed by gzip. ### i. gzcat Lets you look at gzipped file without actually having to gunzip it. ### j. gzip Compresses files. ### k. head Outputs the first 10 lines of file ### l. lpq Check out the printer queue. Example: ### m. lpr Print the file. ### n. lprm Remove something from the printer queue. ### 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. 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).

### q. mv

Moves a file from one location to other.

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.

### 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.

### s. tail

Outputs the last 10 lines of file. Use -f to output appended data as the file grows.

### t. touch

Updates access and modification time stamps of your file. If it doesn’t exists, it’ll be created.

Example:

## 1.2. Text Operations

 awk cut echo egrep fgrep fmt grep nl sed sort tr uniq wc

### a. awk

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

Lets take following file /etc/passwd. Here’s the sample data that this file contains:

Example:

## 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:

To display a value at specific index use following syntax:

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:

Bash has also support for the ternary conditions. Check some examples below.

## 2.3 String Substitution

Check some of the syntax on how to manipulate strings

## 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.

Example:

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.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.

Sometime if conditions becoming confusing so you can write the same condition using the case statements.

Expression Examples:

## 2.6. Loops

There are three types of loops in bash. for, while and until.

Different for Syntax:

while Syntax:

until Syntax:

# 3. Tricks

## Set an alias

Open bash_profile by running following command vim ~/.bash_profile

alias dockerlogin=’ssh www-data@adnan.local -p2222’ # add your alias in .bash_profile

## To quickly go to a specific directory

vim ~/.bashrc

export hotellogs=”/workspace/hotel-api/storage/logs”

## 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

!!
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:
sudo !!
This would change a mkdir somedir into sudo mkdir somedir

## Exit traps

Make your bash scripts more robust by reliably performing cleanup.

## 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

## 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.

# 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.