Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Bash Shell Online

# Hello World Program in Bash Shell

echo "Hello World!"

price_per_apple=5
myfirstletters=ABC
greetings='Hello     World'

echo "price per apple is : $price_per_apple"
echo "price per apple is : \$$price_per_apple"
echo "first 10 letters are : ${myfirstletters}DEFGHIJ"
echo $greetings
echo "$greetings"


#-------------------------------------------------------------
echo""
echo""
#-------------------------------------------------------------

BIRTHDATE="Jan 1 2000"
Presents=10
BIRTHDAY=`date -d "$BIRTHDATE" +%A`

if [ "$BIRTHDATE" == "Jan 1 2000" ];then
    echo "Birthdate is correct"
fi
if [ $Presents == 10 ];then
    echo "no of present is correct"
fi
if [ $BIRTHDAY == "Saturday" ];then
    echo "you are right,birthday is on $BIRTHDAY"
fi


#-------------------------------------------------------------
echo""
echo""
#-------------------------------------------------------------


my_array=(apple banana "Fruit Basket" Grapes)
echo ${#my_array[@]}
echo ${my_array[2]}
echo ${my_array[@]}
echo ${my_array[${#my_array[@]}-1]}
my_array[4]="Mango"
echo ${#my_array[@]}
echo ${my_array[@]}


#-------------------------------------------------------------
echo""
echo""
#-------------------------------------------------------------


Names=(John Eric Jessica)
Numbers=(1 2 3)
Strings=(Hello World)
no_of_names=${#Names[@]}
second_name=${Names[1]}

echo ${Numbers[@]}
echo ${Strings[@]}
echo "Second Name is : " $second_name


#-------------------------------------------------------------
echo""
echo""
#-------------------------------------------------------------


A=3
B=$((100 * $A + 5))
echo $B


COST_PINEAPPLE=50
COST_BANANA=4
COST_WATERMELON=23
COST_BASKET=1
TOTAL=$(($COST_PINEAPPLE + $COST_BANANA + $COST_WATERMELON + $COST_BASKET))
echo "Total Cost is $TOTAL"


#-------------------------------------------------------------
echo""
echo""
#-------------------------------------------------------------


STRING="this is a string"
echo ${#STRING}  


STRING="this is a string"
SUBSTRING="a"
expr index "$STRING" "$SUBSTRING" 


STRING="this is a string"
POS=1
LEN=3
echo ${STRING:$POS:$LEN}


STRING="to be or not to be"
ab=${STRING[@]/be/eat}
echo $ab


STRING="to be or not to be"
ab=${STRING[@]//be/eat}
echo $ab


STRING="to be or not to be"
echo ${STRING[@]// not/}


STRING="to be or not to be"
echo ${STRING[@]/to be/eat now}


STRING="to be or not to be"
echo ${STRING[@]/%be/eat}


STRING="to be or not to be"
echo ${STRING[@]/%be/be on $(date +%Y-%m-%d)} 


BUFFETT="Life is like a snowball. The important thing is finding wet snow and a really long hill."

    # write your code here
    ISAY=$BUFFETT
    change1=${ISAY[@]/snow/foot}
    echo "change1 is : $change1"
    change2=${change1[@]//snow/}
    echo "change2 is : $change2"
    change3=${change2[@]/finding/getting}
    echo "change3 is : $change3"
    loc=`expr index "$change3" 'w'`
    echo "loc is : $loc"
    ISAY=${change3::$loc+2}

# Test code - do not modify
echo "Warren Buffett said:"
echo $BUFFETT
echo "and I say:"
echo "$ISAY"


#-------------------------------------------------------------
echo""
echo""
#-------------------------------------------------------------


NAME="John"
if [ "$NAME" = "John" ]; then
    echo "my name is indeed John"
fi

NAME="Bill"
if [ "$NAME" == "John" ]; then
    echo "My name is John"
else
    echo "My name is not John"
fi


NAME="GEORGE"
if [ "$NAME" = "JOHN" ]; then
    echo "my name is JOHN"
elif [ "$NAME" = "GEORGE" ]; then
    echo "my name is George"
else 
    echo "my name is not John or George"
fi


mycase=1
case $mycase in
    1) echo "it is bash";;
    2) echo "it is perl";;
    3) echo "it is python";;
    4) exit
esac


NUMBER=16
APPLES=16
KING="LUIS"
if [ $NUMBER -gt 15 ] ; then
  echo 1
fi
if [ $NUMBER -eq $APPLES ] ; then
  echo 2
fi
if [[ ($APPLES -eq 12) || ("$KING" = "LUIS") ]] ; then
  echo 3
fi
if [[ $(($NUMBER + $APPLES)) -le 32 ]] ; then
  echo 4
fi


NAMES=(Joe Jenny Sara Tony)
for N in ${NAMES[@]} ; do
    echo "my name is : $N"
done


COUNT=4
while [ $COUNT -gt 0 ]; do
    echo "Value of count is : $COUNT"
    COUNT=$(($COUNT - 1))
done


COUNT=1
until [ $COUNT -gt 5 ]; do
    echo "count is : $COUNT"
    COUNT=$((COUNT + 1))
done


COUNT=0
while [ $COUNT -ge 0 ]; do
    echo "Count is jay : $COUNT"
    COUNT=$(($COUNT+1))
    if [ $COUNT -ge 5 ]; then
        break
    fi
done


COUNT=0
while [ $COUNT -lt 10 ]; do
    COUNT=$(($COUNT+1))
    if [ $(($COUNT % 2)) = 0 ]; then
        echo "Even numbers are $COUNT"
        continue
    fi
done


NUMBERS=(951 402 984 651 360 69 408 319 601 485 980 507 725 547 544 615 83 165 141 501 263 617 865 575 219 390 237 412 566 826 248 866 950 626 949 687 217 815 67 104 58 512 24 892 894 767 553 81 379 843 831 445 742 717 958 609 842 451 688 753 854 685 93 857 440 380 126 721 328 753 470 743 527)
for num in ${NUMBERS[@]}; do
    if [ $num == 237 ]; then
        break;
    elif [ $(($num % 2)) == 0 ]; then
        echo "Even number is : $num"
    fi
done


#-------------------------------------------------------------
echo""
echo""
#-------------------------------------------------------------


function func1 {
    echo "Function1"
}
function func2 {
    echo "$1,$2"
}
function func3 {
    echo "$(($1 + $2))"
}

func1
func2 "Function2" "Function3"
func3 10 2


function cal {
    a=$1
    signn=$2
    b=$3
      if [ $signn == "plus" ]; then
        echo "$a + $b = $(($a+$b))"
      elif [ $signn == "minus" ]; then
        echo "$a - $b = $(($a-$b))"
      elif [ $signn == "multiply" ]; then
        echo "$a * $b = $(($a*$b))"
      fi
}
cal 22 plus 11
cal 22 minus 11
cal 2 multiply 4


#-------------------------------------------------------------
echo""
echo""
#-------------------------------------------------------------


filename="sample.txt"
if [ -e $filename ]
    

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.