Linux shell script program to swap two numbers

Here, we are going to learn how to swap two numbers in Linux shell script?
Submitted by Nidhi, on February 14, 2021

Problem Solution:

Here, we will create a shell script program to swap to numbers and then print both variables after swapping on the console screen.

Program/Source Code:

The source code to create a Linux shell script program to swap two numbers is given below. The given program is compiled and executed successfully on Ubuntu 20.04.


# Program name: ""
# shell script program to swap two numbers.


echo "Before Swapping"
echo "Num1: $num1"
echo "Num2: $num2"


echo "After Swapping"
echo "Num1: $num1"
echo "Num2: $num2"

Now we will save the shell script program with the "" name.


$ sh 
Before Swapping
Num1: 10
Num2: 20
After Swapping
Num1: num2
Num2: num3


In the above program, we created two variables num1 and num2 that are initialized with 10 and 20 respectively. Here, we interchanged the values of both variables using variable num3 and then print both variables on the console screen.

Linux shell script programs »


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.