Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace tab with space using Bash parameter substitution

Tags:

bash

replace

How can I use ${var//Pattern/Replacement} to replace tab with space?

${var//\t/ } doesn't work for me.

like image 933
Roovent Avatar asked Nov 20 '16 21:11

Roovent


People also ask

How do I replace tabs with spaces?

Instead of changing tabs to spaces one by one, the Word's Find and Replace function is commonly used to convert tabs to spaces. Step 3: Enter a space character (press space button on your keyboard) in the Replace With field; Step 4: Click Replace All.

How do I replace a tab with 4 spaces in vim?

With 'expandtab' on, Vim replaces all tabs with the appropriate number of spaces. Note that the command accepts a range, so you can make a visual selection and then just :retab the selected lines. Save this answer.

How do I change tab to space in Linux?

To convert tabs in a text file to spaces, you can use the expand command. To convert each tab to a single space character, use the -t 1 option.


1 Answers

Adding here since typing an actual tab character through PuTTY into a bash shell is not so simple. CTRL V TAB doesn't work since CTRL V is captured in windows as paste.

Instead I use:

${var//$'\t'/ }

Example:

$ var="te"$'\t'"st"

$ echo $var
te st

$ echo ${var//$'\t'/i}
teist
like image 75
JNevill Avatar answered Sep 21 '22 17:09

JNevill