oneohthree
oneohthree

Reputation: 51

Use heredoc inside $(...) instead of `...`

Right now I have this:

db_conn="mysql -sN -u$db_user -p$db_user_passwd -h$db_host -P$db_port -D$db_name"

query=`$db_conn <<-QRY
    SELECT foo
    FROM bar
    WHERE baz = 1;
QRY`

But I hate to use backticks for calling commands. How can I write this using the form $(...)?

Upvotes: 1

Views: 62

Answers (2)

ArturFH
ArturFH

Reputation: 1787

Insert newline after heredoc delimiter:

query=$($db_conn <<-QRY
    SELECT foo
    FROM bar
    WHERE baz = 1;
QRY
)

Upvotes: 1

stark
stark

Reputation: 13189

You can use backslashes:

db_conn="mysql -sN -u$db_user -p$db_user_passwd -h$db_host -P$db_port -D$db_name"

query=$($db_conn \
    SELECT foo \
    FROM bar \
    WHERE baz = 1; \
)

Of course this means you don't have newlines in the resulting string, unless you add them yourself.

Upvotes: 1

Related Questions