Bash Shell: Convert Tabs To Spaces In a File

How do convert tabs to spaces in a file using a bash shell?

You can use various tools. The expand command converts all tabs to spaces. It preserves backspace characters in the output; they decrement the column count for
tab calculations.

expand input.file > output.file
expand data.txt > output.txt
expand -t 2 data.txt > output.txt

The -t option can be used to set comma separated list of explicit tab positions. You can use the unexpand command convert spaces to tabs. See man page for more info:
man expand
man unexpand

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • Tapas Mallick Mar 6, 2010 @ 15:03

    My imput.file looks like:
    $ cat input.file
    tux Linux
    tux Linux
    tux Linux
    tux Linux
    But executing expand on the file showing the same format(in STDOUT) as input.file…
    $ expand input.file
    tux Linux
    tux Linux
    tux Linux
    tux Linux
    Though I am getting the expected result with “tr” command.
    I am using CentOS 5.3(x86_64); Any comment ?

    • Tom De Maesschalck Jul 29, 2015 @ 8:20

      That’s because expand expects 2 tabs (=8 charachter spaces) as default tab.
      So it doesn’t a tab in your file.
      The command expand -t1 wil work with your file, as t1 means 4 spaces

  • stealth Mar 9, 2010 @ 13:33

    You can also use perl:
    perl -pi -e ‘s/\t/ /’

  • Gen2ly Mar 10, 2010 @ 5:35


    for vim.

  • Tapas Mallick Mar 17, 2010 @ 8:26

    s are truncated in my posts itself :)

  • Towo Mar 14, 2013 @ 15:17

    expand fails miserably on multibyte characters (i.e. on non-ASCII characters in UTF-8 encoding)
    still as of coreutils 8.21 (2013)

  • Dixon Apr 2, 2014 @ 5:37

    Its working for me
    Tab space conver into space
    expand -t 2 file | mailx -s “xxxx”

  • Nick Feb 23, 2015 @ 21:14

    expand doesn’t seem to do the right thing.
    Converting tabs to spaces in a piece of content is more than merely translating each tab character to a sequence of space characters. It needs to preserve the alignment of the text. So in this example text (no quotes):
    and with a tab spacing of 3 chars, the result should be (each period corresponds to a white space):
    This way the de-tabbed content would overlay the original content exactly (assuming the same tab setting).

  • Nick Feb 23, 2015 @ 22:23

    Typo in my example, and not a great example. Let’s try this one instead:
    and with a tab spacing of 4 chars, the result should be (each period corresponds to a white space):

    Note how each tab char was not replaced with 4 spaces.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum