Just getting to know how to do stuff in bash

Hello World!!

Always start the script with a shebang : output

Variables and Comments :

Anything after a # symbol : output

Capital Case Variables and System Defined Info: output

Using Variables : output

  1. No space between the variable and value with = operator.
  2. Refer the variable using $ operator.
  3. Can’t use variable name starting with a number

Read User Input : output

Multiple Reads : output

Get Input in Same Line : output

Read Password and keep them hidden on screen : output

Read var-args and store in array : output

Read with No Variable : output

read puts the values read in default variable : “REPLY”

Passing arguments to script :

Passing arguments separately :

These are accessed using numbered values like :

$1 for arg1

$2 for arg2. so on

Passing arguments and using as array :

caveats :

Conditionals :

Numeric Comparisons : output

String Comparisons : output

IF-ELSE blocks : output

File Operators :

check if file exists :

Operators :

Logical Operators : output

AND is used as && or with -a

OR is similarly used as || or with -o

Using && and separate predicate blocks.

Using -a for AND : output

Using && and [[ multiple conditions ]] : output

Arithmetic Expressions :

By default variables are treated as strings in bash scripts.

But parsing a string to numbers is very easy using double parentheses and external command such as expr (evaluate Expressions) OR bc (Basic Calculator).

integer arithmetic

using + for Addition, — for Subtraction, * for Multiplication, / for Division and % for modulus. : output

using expr (integer arithemetic) : output

It does not work with floating point number

floating point arithmetic using bc :

bc : basic calculator, its a language in itself.

More complex compute : example

Case Statements :

The case statement is used to execute statements based on specific values.

Often used in place of an if statement, if there are a large number of conditions.

The variable is compared with the values using the shell wildcards ( ? * […] ) , NOT regular expressions.

All the statements are executed for the first matching value until the ending. If no value matches, then the default * case is executed, if present.

simple example :

* is a wildcard to match everything else

what if we add same case with diff block to execute :

What if I change value as I execute the block :

example of case with patterns : (can show diff output with diff versions of bash)

Arrays :

Basic bash support single one dimensional Arrays : output

Array unset operation : unset can change length of array. : output

String as Array : output

Loops :

While Loop :

Iterate over 1 to 10 : output

Bit more simpler : Iterate over 1 to 10 leaving 2 : output

Until Loop :

example : output

For Loop :

Method 1 : space separated values to iterate on.

Method 2 : Loop over 1 to N, bash version > 3.0.0

Method 3 : Loop over 1 to N with difference of D, bash version >4.0.0

Method 4 : iterate over output of a command

Method 5 : Expression based

Example of Advanced Usage :

Read File :

Method 1 : Input redirection : Data in file is redirected to this while loop.

Method 2 : read file in single variable and then print it.

we are using the cat utility here.

Note : sometime its hard to read this file due to eg. line indentation, special character, escape character

Method 3 : using IFS (Internal Field separator)

Select :

use case : selecting stuff from menu

Method 1 : Simply selecting stuff

Method 2 : Select with case

Break and Continue statements :

Break :

Continue :

Functions :

abstracting logic

  1. No Argument function :

2. One or more argument function :

3. Variadic function :

Use Case : using function to build abstractions :

you want to exit and before you exit, you want to print or log a statement :

Now, one can use this quit function directly and won’t have to repeatedly write the statement.

Use Case : using variables in functions :

you will have to use a keyword : local to define local variables, otherwise the local declared variables are not scoped to be local by default, rather are scoped to be global.

Correct Way : this is continued from previous code snippet

use case : Return by functions