DEV Community

tarohida
tarohida

Posted on

1 2

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay