Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Bash shell script to swap two numbers

  • Difficulty Level : Basic
  • Last Updated : 05 Jul, 2018

Given two numbers and the task is to swap them using the third variable.


Input: first = 2, second = 4
Output: first = 4, second = 2

Input: first = 5, second = 7
Output: first = 7, second = 5


  1. Store the value of the first number into a temp variable.
  2. Store the value of the second number in the first number.
  3. Store the value of temp into the second variable.

# !/bin/bash
# Program to swap two numbers
# Static input of the
# number
echo "After swapping, numbers are:"
echo "first = $first, second = $second"


After swapping, numbers are:
first = 10, second = 5

How to execute the bash files?

  1. Write the bash code into a file and save that file with .sh extension i.e.
  2. Open terminal and execute the file using below command:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!