Given two numbers and the task is to swap them using the third variable.
Examples:
Input: first = 2, second = 4 Output: first = 4, second = 2 Input: first = 5, second = 7 Output: first = 7, second = 5
Approach:
- Store the value of the first number into a temp variable.
- Store the value of the second number in the first number.
- Store the value of temp into the second variable.
# !/bin/bash # Program to swap two numbers # Static input of the # number first=5 second=10 temp=$first first=$second second=$temp echo "After swapping, numbers are:"echo "first = $first, second = $second" |
chevron_right
filter_none
Output:
After swapping, numbers are: first = 10, second = 5
How to execute the bash files?
- Write the bash code into a file and save that file with .sh extension i.e. filename.sh
- Open terminal and execute the file using below command:
./filename.sh

Recommended Posts:
- Bash shell script to find sum of digits
- Bash shell script to find out the largest value from given command line arguments
- Leap Year Program with Bash Shell Scripting in Windows
- Introduction to Linux Shell and Shell Scripting
- Write a bash script to print a particular line from a file
- Bash Script to get Low Battery Alert in Linux
- Looping Statements | Shell Script
- Conditional Statements | Shell Script
- String Operators | Shell Script
- Implementing Directory Management using Shell Script
- Automated Recursive Encryption in a Directory Using Shell Script
- Difference Between Single and Double Quotes in Shell Script and Linux
- Average of given numbers in Bash
- Bash program to check if the Number is a Prime or not
- Fibonacci Series in Bash
- Useful and time saving bash commands in Linux
- Bash program to find A to the power B
- Bash program to check if the Number is a Palindrome
- Sorting an array in Bash using Bubble sort
- Simple Calculator in Bash
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

