Let us look into how to write shell scripts to:

1. Find the sum of the n natural number
2. Swap the two values using only two variables
3. To find the sum of n even numbers
4. To print the multiplication table of a given number

Shell scripting is programming through which we write several lines of command together to execute. We can either have extension .sh, .bash, etc. depending on the type of shell we use.

Here, let us look into shell scripts with extension .sh which is used with the Bourne Shell.

## 1. Find the sum of the n natural number

### Sample Code:

```.wp-block-code{border:0;padding:0;-webkit-text-size-adjust:100%;text-size-adjust:100%}.wp-block-code>span{display:block;overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc:before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```#to find sum of n numbers
#! /bin/sh
echo "Enter n"
i=1
sum=0
while [ \$i -le \$n ]
do
sum=\$(expr \$sum + \$p)
i=\$(expr \$i + 1)
done
echo \$sum```Code language: PHP (php)```

## 2. Swap the two values using only two variables

### Sample Code:

``````#!bin/sh
echo 'Enter value of x'
echo 'Enter value of y'
y=\$((y+x))
x=\$((y-x))
y=\$((y-x))
echo "The value of x and y after swap is x=\$x and y=\$y"```Code language: JavaScript (javascript)```

## 3. To find the sum of n even numbers

### Sample Code:

``````#! /bin/sh
#sum of even numbersE
sum=0
for ((i=1;i&lt;=n;i++))
do
if ((i%2==0))
then
sum=\$((sum+i))
fi
done
echo \$sum```Code language: PHP (php)```

## 4. To print the multiplication table of a given number

### Sample Code:

``````#!bin/sh
i=1;
echo 'Enter the multiplier number'
echo 'Enter the limit of the multiplication number'
echo "The multiplication table of \$n":
for ((i=1;i&lt;=limit;i++))
do
pro=\$((n*i))
echo "\$n * \$i = \$pro"
done```Code language: PHP (php)```