Skip to main content
Asked
Viewed 3k times
13

I was trying to execute new line using echo and tried following two commands:

  1. First command:

    echo $'Hello World\nThis is a new line'
    

    Response:

    Hello World
    This is a new line
    
  2. Second command:

    echo $"Hello World\nThis is a new line"
    

    Response:

    Hello World\nThis is a new line
    

My question is what's the difference between string wrapped with $' ' vs string wrapped with $" " in bash's echo?

4
  • Helpful note : Double quote is also known as weak quoting and Single quote is also known as strong quoting. Commented Aug 18, 2016 at 4:29
  • 2
    Yes, but "weak quote" and 'strong quote' are different from $'C String' and $"I18N String".
    – DopeGhoti
    Commented Aug 18, 2016 at 4:30
  • You the real MVP. :D >>> @DopeGhoti Commented Aug 18, 2016 at 4:32
  • I do what I can (:
    – DopeGhoti
    Commented Aug 18, 2016 at 4:33

2 Answers 2

11

As explained here, the syntax $'string' specifies a C-style string which includes magic escaped characters, such as \n for a newline. $"string" is for I18N expansion, which has no such magic escapes.

Note that these are distinct from the more common "string" (weak quoting) and 'string' (strong quoting).

9

The $ in the beginning of the string in :

echo $'Hello World\nThis is a new line'

causes escape sequences to be interpreted.

Bash reference manual [ says ]

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. ..
..
The expanded result is single-quoted, as if the dollar sign had not been present.

But

echo $"Hello World\nThis is a new line"

is completely different. This [ article ] on locale specific translation says :

A double-quoted string preceded by a dollar sign (‘$’) will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.


Note: IIRC both $'string' and $"string" may not find support among different shells. Not only do people from other shells look at them with curiosity but also they debate on whether this could be avoided for script portability.

4
  • 1
    ksh93 also understands $'...'.
    – Kusalananda
    Commented Aug 18, 2016 at 4:53
  • @Kusalananda : Hmm, Good note :)
    – sjsam
    Commented Aug 18, 2016 at 8:30
  • 4
    $'xxx' is not a bashism but was first introduced by ksh several years before bash did. It is currently also supported by zsh and busybox sh and is under review by the POSIX standard.
    – jlliagre
    Commented Aug 18, 2016 at 8:35
  • @jlliagre : Appreciate the time :) . I stand corrected..
    – sjsam
    Commented Aug 18, 2016 at 8:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .

string' and double quoted $"string" in bash? - Unix & Linux Stack Exchange">
Skip to main content
Asked
Viewed 3k times
13

I was trying to execute new line using echo and tried following two commands:

  1. First command:

    echo $'Hello World\nThis is a new line'
    

    Response:

    Hello World
    This is a new line
    
  2. Second command:

    echo $"Hello World\nThis is a new line"
    

    Response:

    Hello World\nThis is a new line
    

My question is what's the difference between string wrapped with $' ' vs string wrapped with $" " in bash's echo?

4
  • Helpful note : Double quote is also known as weak quoting and Single quote is also known as strong quoting. Commented Aug 18, 2016 at 4:29
  • 2
    Yes, but "weak quote" and 'strong quote' are different from $'C String' and $"I18N String".
    – DopeGhoti
    Commented Aug 18, 2016 at 4:30
  • You the real MVP. :D >>> @DopeGhoti Commented Aug 18, 2016 at 4:32
  • I do what I can (:
    – DopeGhoti
    Commented Aug 18, 2016 at 4:33

2 Answers 2

11

As explained here, the syntax $'string' specifies a C-style string which includes magic escaped characters, such as \n for a newline. $"string" is for I18N expansion, which has no such magic escapes.

Note that these are distinct from the more common "string" (weak quoting) and 'string' (strong quoting).

9

The $ in the beginning of the string in :

echo $'Hello World\nThis is a new line'

causes escape sequences to be interpreted.

Bash reference manual [ says ]

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. ..
..
The expanded result is single-quoted, as if the dollar sign had not been present.

But

echo $"Hello World\nThis is a new line"

is completely different. This [ article ] on locale specific translation says :

A double-quoted string preceded by a dollar sign (‘$’) will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.


Note: IIRC both $'string' and $"string" may not find support among different shells. Not only do people from other shells look at them with curiosity but also they debate on whether this could be avoided for script portability.

4
  • 1
    ksh93 also understands $'...'.
    – Kusalananda
    Commented Aug 18, 2016 at 4:53
  • @Kusalananda : Hmm, Good note :)
    – sjsam
    Commented Aug 18, 2016 at 8:30
  • 4
    $'xxx' is not a bashism but was first introduced by ksh several years before bash did. It is currently also supported by zsh and busybox sh and is under review by the POSIX standard.
    – jlliagre
    Commented Aug 18, 2016 at 8:35
  • @jlliagre : Appreciate the time :) . I stand corrected..
    – sjsam
    Commented Aug 18, 2016 at 8:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .