Bash

Killing processes

kill process in bash

Basics of bash

Notes

Sample script 1:

#!/bin/bash

echo "hi there"
echo "my current directory is "
pwd

echo "contents of the directory is"
ls

Variables in bash

Example 1

#!/bin/bash

a=3
b=4 
c= $a + $b 
echo "$c"

output:
3+4

Example 2

#!/bin/bash

word="fun"

echo "linux is $word"
echo "bash is $word"
echo "Test cricket is $word"

Output:

linux is fun
bash is fun
Test cricket is fun

Example 3 (Capturing output of a command)

#!/bin/bash
echo "file contents are $(ls)"
echo "present directory is $(pwd)}"

now=$(date) 
echo "system date and time is $now"

name=$(whoami) # or use $(USER)
echo "my name is $name and using this machine on $now"


Math functions

Example 1

#!/bin/bash

var=33
total= expr $var + 4
echo "total is $total"


Here are some ways to add two numbers and store the result in a variable in Bash:
Using the let command.
Code

num1=10
num2=20
let sum=$num1+$num2
echo "The sum is $sum"

This will print the following output:
Code
The sum is 30

Using the expr command.

num1=10
num2=20
sum=$(expr $num1 + $num2)
echo "The sum is $sum"

This will also print the following output:

Code
The sum is 30

Using the bc command.

num1=10
num2=20
sum=$(echo $num1 + $num2 | bc)
echo "The sum is $sum"

This will also print the following output:
The sum is 30