0
0
Bash-scriptingConversionBeginner · 2 min read

Bash Script to Convert Tabs to Spaces Easily

Use the command expand -t 4 inputfile > outputfile in Bash to convert tabs to spaces, where -t 4 sets 4 spaces per tab; or use sed $'s/\t/ /g' to replace tabs with spaces directly in a script.
📋

Examples

InputHello World
OutputHello World
InputLine1 Line2 Line3
OutputLine1 Line2 Line3
Input Start with tab
Output Start with tab
🧠

How to Think About It

To convert tabs to spaces in Bash, think of tabs as special characters that move the cursor to the next tab stop. We want to replace each tab character with a fixed number of spaces, usually 4. The simplest way is to use a tool that understands tabs and can expand them, or replace tabs with spaces directly by searching for the tab character and substituting it.
📐

Algorithm

1
Read the input text line by line.
2
For each line, find all tab characters.
3
Replace each tab character with a fixed number of spaces (e.g., 4).
4
Output the modified line.
5
Repeat until all lines are processed.
💻

Code

bash
#!/bin/bash
# Convert tabs to 4 spaces

input="input.txt"
output="output.txt"

expand -t 4 "$input" > "$output"
echo "Tabs converted to spaces in $output"
Output
Tabs converted to spaces in output.txt
🔍

Dry Run

Let's trace converting the line 'Hello\tWorld' using expand -t 4.

1

Input line

Hello\tWorld

2

expand command processes tabs

Each tab is replaced by spaces to reach next tab stop (4 spaces)

3

Output line

Hello World

InputOutput
Hello\tWorldHello World
💡

Why This Works

Step 1: Why use expand?

The expand command is designed to convert tabs to spaces correctly according to tab stops, making it reliable.

Step 2: How tab stops work

Tabs move the cursor to the next multiple of the tab size (default 8, here set to 4), so spaces replace tabs to align text properly.

Step 3: Alternative with sed

Using sed to replace tabs with spaces works but does not consider tab stops, so spacing may not align perfectly.

🔄

Alternative Approaches

sed substitution
bash
#!/bin/bash
sed $'s/\t/    /g' input.txt > output.txt
echo "Tabs replaced with spaces using sed"
Simple replacement but does not handle tab stops, so alignment may be off if tabs are uneven.
tr command
bash
#!/bin/bash
tr '\t' ' ' < input.txt > output.txt
echo "Tabs replaced with single spaces using tr"
Replaces each tab with a single space only, not multiple spaces, so not ideal for alignment.

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

Time Complexity

The script processes each character once, so time grows linearly with input size.

Space Complexity

Output requires space proportional to input size since tabs expand into multiple spaces.

Which Approach is Fastest?

Using expand is efficient and accurate; sed is simpler but less precise; tr is fastest but only replaces tabs with single spaces.

ApproachTimeSpaceBest For
expand -t 4O(n)O(n)Accurate tab to spaces with alignment
sed substitutionO(n)O(n)Simple replacement without alignment
tr commandO(n)O(n)Fast replacement with single space
💡
Use expand -t 4 for accurate tab to space conversion respecting tab stops.
⚠️
Replacing tabs with a fixed number of spaces without considering tab stops can misalign text.