git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Improve sed portability

From
CRChris Ridd <chris.ridd@isode.com>
Date
Jun 12, 2008, 08:29 UTC
Message-ID
<4850DE67.703@isode.com>
In-Reply-To
<4850D45E.8000802@viscovery.net>
Johannes Sixt wrote:
Show 29 quoted lines
> Chris Ridd schrieb:
>> Johannes Sixt wrote:
>>> Chris Ridd schrieb:
>>>> @@ -73,7 +73,7 @@ resolve_relative_url ()
>>>>  module_name()
>>>>  {
>>>>      # Do we have "submodule.<something>.path = $1" defined in
>>>> .gitmodules file?
>>>> -    re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
>>>> +    re=$(printf "%s\n" "$1" | sed -e 's/[].[^$\\*]/\\&/g')
>>> You change sq into dq. Is this not dangerous? Shouldn't backslash-en be
>>> hidden from the shell so that printf can interpret it?
>> It is necessary to use double quotes. This:
>>
>>     printf '%s\n' foobar
>>
>> prints a literal \, a literal n, and no newline:
>>
>>     foobar\n
>>
>> Not desirable :-(
> 
> On both Linux and AIX 4.3 I see:
> 
> $  printf 'x\ny'; echo z
> x
> yz
> 
> The printf turns the \n into LF.

Yes, and I don't know *what* I did yesterday, but Solaris 8, 10, (every OS I mentioned before) behave the same as your test.

I did actually have my eyes tested later on yesterday :-)
> I mentioned this in the first place because I don't know what various
> shells do with \n when they see "%s\n". But one way or the other, the \n
> will be turned into LF, either by the shell or by printf. So it's not a
> big deal.
I agree.
Show 8 quoted lines
>> Of course, using a plain old:
>>
>>     echo "$1"
>>
>> should work well too. Why is printf being used here and not echo, anyway?
> 
> Because the "$1" could contain character sequences that some 'echo'
> implementations mangle.
Indeed. If $1 started with -n that might cause problems on some platforms.
Should I revise my commit to use single quotes again?
Cheers,
Chris
Previous: Johannes SixtNext: Jeff King
Message 6 of 9 in “Improve sed portability”
  1. Improve sed portabilityChris Ridd, Jun 11, 2008
  2. Johannes SixtJun 11, 2008
  3. Chris RiddJun 11, 2008
  4. Jeff KingJun 11, 2008
  5. Johannes SixtJun 12, 2008
  6. Chris RiddJun 12, 2008
  7. Jeff KingJun 12, 2008
  8. Jakub NarebskiJul 13, 2008
  9. Junio C HamanoJun 12, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.