Shell Script to swap values in two variables x and y

in Categories Shell Math last updated April 9, 2008


Share this on:

5 comment

  1. This may be a simpler of all!
    echo ENTER TWO VARIABLES
    read a b
    t=expr $a
    a=expr $b
    b=expr $t
    echo EXCHANGED VARIABLES ARE $a AND $b
    exit 0

    1. hi, can you plz explain what that “<<<" sign does…? "<<" is here document and what that thirs "<" sign does…? plz help..

  2. Try this option, we can avoid the third variable

    #!/bin/bash
    echo -e “Type the value of x : ”
    read x
    echo -e “Type the value of y : ”
    read y
    x=expr $x + $y
    y=expr $x - $y
    x=expr $x - $y

    echo -e “The value of x :$x ”
    echo -e “The value of y :$y “

    Have a question? Post it on our forum!