DEV Community

tarohida
tarohida

Posted on

When I expand shell variable which have tab chars or space chars, how it works.

Environment

  • bash

Problems

When I defined shell values such below, how is space chars or tab chars in variable processed?

packages='
    httpd
    postgresql
    redis
'

yum install ${packages}
Enter fullscreen mode Exit fullscreen mode

How to solve

It is expanded to yum httpd postgresql redis, and it works as expected.

Description

Let's see how it is expanded with using echo command.

#!/bin/bash
# your code goes here

packages='
    httpd
    postgresql
    redis
'

echo ${packages}
echo '${packages}'
echo "${packages}"
Enter fullscreen mode Exit fullscreen mode

output

httpd postgresql redis
${packages}

    httpd
    postgresql
    redis
Enter fullscreen mode Exit fullscreen mode

https://ideone.com/XfbkBI

When I quote variable with double quote, it expand, but tab characters or space characters does not replaced with single space.

Top comments (0)