lxvs
lxvs

Reputation: 1030

Nested double-quotes in Bash scripts

When I writing bash scripts, I found below statements valid and work as expected:

#!/bin/bash
fn="file name"
LF='
'
fnc="$LF$(cat "$fn")"
echo "$fnc"

But there is a pair of unattended nested quotes in fnc="$LF$(cat "$fn")". Is this a feature, or luck?

Upvotes: 1

Views: 885

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

It is a feature all POSIX shells have, briefly described in the standard as below.

The input characters within the quoted string that are also enclosed between $( and the matching ) shall not be affected by the double-quotes[.]

Which means, in this particular case, if you unquote "$fn" inside the command subtitution, it will be subjected to word splitting regardless, and cat will receive two arguments (file and name) instead of one (file name). Whether $(cat $fn) is between double-quotes or not does not affect how $fn is expanded.

Upvotes: 7

Related Questions