Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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'
    

    Respose:

    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 echo?

share|improve this question
    
Helpful note : Double quote is also known as weak quoting and Single quote is also known as strong quoting. – Rafaf Tahsin 3 hours ago
1  
Yes, but "weak quote" and 'strong quote' are different from $'C String' and $"I18N String". – DopeGhoti 3 hours ago
    
You the real MVP. :D >>> @DopeGhoti – Rafaf Tahsin 3 hours ago
    
I do what I can (: – DopeGhoti 3 hours ago
up vote 5 down vote accepted

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).

share|improve this answer

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" as bashisms [ needs validation ]. Not only do people from other shells look at them curiosity but also they debate on whether this could be avoided for script portability.

share|improve this answer
    
ksh93 also understands $'...'. – Kusalananda 3 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.