0
0
Bash-scriptingConversionBeginner · 2 min read

Bash Script to Convert Decimal to Binary Number

Use a Bash script with a loop and bitwise operations like while [ $decimal -gt 0 ]; do binary=$((decimal % 2))$binary; decimal=$((decimal / 2)); done to convert decimal to binary.
📋

Examples

Input0
Output0
Input5
Output101
Input18
Output10010
🧠

How to Think About It

To convert decimal to binary, repeatedly divide the decimal number by 2 and record the remainder each time. These remainders, read in reverse order, form the binary number. Stop when the decimal number becomes zero.
📐

Algorithm

1
Get the decimal input number
2
If the number is zero, return 0
3
While the number is greater than zero:
4
Find remainder of number divided by 2
5
Prepend remainder to the binary result string
6
Divide the number by 2 (integer division)
7
Return the binary result string
💻

Code

bash
#!/bin/bash
read -p "Enter a decimal number: " decimal
if [ "$decimal" -eq 0 ]; then
  echo 0
  exit
fi
binary=""
while [ $decimal -gt 0 ]; do
  remainder=$((decimal % 2))
  binary="$remainder$binary"
  decimal=$((decimal / 2))
done
echo "$binary"
Output
Enter a decimal number: 18 10010
🔍

Dry Run

Let's trace the decimal number 5 through the code

1

Initial input

decimal = 5, binary = ""

2

First loop iteration

remainder = 5 % 2 = 1, binary = "1", decimal = 5 / 2 = 2

3

Second loop iteration

remainder = 2 % 2 = 0, binary = "01", decimal = 2 / 2 = 1

4

Third loop iteration

remainder = 1 % 2 = 1, binary = "101", decimal = 1 / 2 = 0

5

Loop ends

decimal = 0, final binary = "101"

decimalremainderbinary
511
2001
11101
💡

Why This Works

Step 1: Divide and get remainder

Each division by 2 gives a remainder of 0 or 1, which is a binary digit.

Step 2: Build binary string

Prepending the remainder builds the binary number from least significant bit to most.

Step 3: Stop when zero

When the decimal number reaches zero, all binary digits are collected.

🔄

Alternative Approaches

Using printf with built-in conversion
bash
read -p "Enter decimal: " decimal
printf "%b\n" "$(echo "obase=2; $decimal" | bc)"
Uses external bc command for conversion; simpler but requires bc installed.
Using Bash built-in printf with %d and %b
bash
read -p "Enter decimal: " decimal
printf '%d in binary is %s\n' "$decimal" "$(echo "obase=2; $decimal" | bc)"
Combines input and output formatting; still depends on bc.

Complexity: O(log n) time, O(log n) space

Time Complexity

The loop runs once for each binary digit, which is proportional to log base 2 of the decimal number.

Space Complexity

The binary string grows with the number of bits, so space is proportional to log base 2 of the input.

Which Approach is Fastest?

The manual loop is efficient and requires no external tools; using bc is simpler but depends on external command.

ApproachTimeSpaceBest For
Manual loop with divisionO(log n)O(log n)No dependencies, educational
Using bc commandO(log n)O(log n)Quick and simple if bc is available
💡
Use a loop dividing by 2 and collecting remainders to convert decimal to binary in Bash.
⚠️
Forgetting to prepend remainders causes the binary digits to be reversed.