DEV Community

loading...

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

tarohida profile image tarohida ・1 min read

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.

Discussion (0)

pic
Editor guide