bash script\ code example

Example 1: $@ bash

file:test.sh
#! /bin/sh
echo '$#' $#
echo '$@' $@
echo '$?' $?

*If you run the above script as*

> ./test.sh 1 2 3

You get output:
$#  3
$@  1 2 3
$?  0

*You passed 3 parameters to your script.*

$# = number of arguments. Answer is 3
$@ = what parameters were passed. Answer is 1 2 3
$? = was last command successful. Answer is 0 which means 'yes'

Example 2: bash script loop

while [ <some test> ]
do
<commands>
done

Example 3: bash scripting

#!/bin/sh

# Author : Zara Ali
# Copyright (c) Tutorialspoint.com
# Script follows here:

echo "What is your role"
read ROLE
echo "My Role : $ROLE"

Example 4: bash "=~" example

$ [[ 45 =~ [0-9]+ ]] && echo "45 contains digits"
45 contains digits

$ [[ "hello" =~ [0-9]+ ]] && echo "hello doesnt contains digits"
$ [[ "hello" =~ [a-z]+ ]] && echo "hello contains alphabets"
hello contains alphabets

Example 5: shell script linux

#!/bin/bash
echo "Hello World!"

Example 6: linux bash scripts tutorial

$ which bash