#!/usr/bin/env bash n_spaces="$1" if [ ! "$n_spaces" ]; then n_spaces=4 fi if [ $n_spaces -lt 1 ]; then echo 'Sorry. Number of spaces should be a positive number' exit 1 fi space='' i=0 while [ true ]; do space="$space " i=$(( $i + 1 )) if [ $i -eq $n_spaces ]; then break fi done while IFS='' read -r line || [[ -n "$line" ]]; do if [[ ${line:0:1} != ' ' ]]; then echo "$line" continue fi # Line length line_len=${#line} # Get leading tabs and their length tabs=$(echo "$line" | sed -n 's/^\( *\)\(.*\)$/\1/p') tabs_len=${#tabs} line_without_tabs_len=$(( $line_len - $tabs_len )) line_without_tabs=${line:$tabs_len:$line_without_tabs_len} spaces=$(echo "$tabs" | sed "s/ /$space/g") echo "$spaces$line_without_tabs" done