Bash: Keeping indentation during interpolation -


i have variable containing multiline string.

i going interpolate variable multiline echoed string, echoed string has indentation.

here's example:

ip_status=`ip -o addr | awk 'begin {  printf "%-12s %-12s %-12s\n", "interface", "protocol", "address"                                       printf "%-12s %-12s %-12s\n", "---------", "--------", "-------" }                                    {  printf "%-12s %-12s %-12s\n", $2, $3, $4 }'`  echo -e "     ->     $ip_status     -> " 

when running that, first line of $ip_status left justified against ->, subsequent lines not justified against ->.

it's easier see if run in bash. output:

    ->     interface    protocol     address ---------    --------     ------- lo           inet         127.0.0.1/8 lo           inet6        ::1/128 eth0         inet         10.0.2.15/24 eth0         inet6        fe80::a00:27ff:fed3:76c/64     -> 

i want lines in $ip_status aligned ->, not first line.

echo  "    ->" while ifs= read -r line     echo "    $line" done <<< "$ip_status" echo  "    ->" 

you can read variable line line , echo number of spaces need before it. have used accepted answer of question.


make function:

myfunction() { echo  "    ->" while ifs= read -r line     echo "    $line" done <<< "$1" echo  "    ->" }  myfunction "$ip_status" 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -