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

Re: [PATCH] Improve sed portability

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 12, 2008, 08:33 UTC
Message-ID
<7vy75b833p.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<484FDB5D.7060606@viscovery.net>
Johannes Sixt <j.sixt@viscovery.net> writes:
Show 19 quoted lines
> Chris Ridd schrieb:
>> On Solaris /usr/bin/sed apparently fails to process input that doesn't
>> end in a \n. Consequently constructs like
>> 
>>   re=$(printf '%s' foo | sed -e 's/bar/BAR/g' $)
>> 
>> cause re to be set to the empty string.
>
> So does /usr/bin/sed of AIX 4.3!
>
>> @@ -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?

"\n" inside dq is _not_ interpreted by the shell (printf interprets it), but I tend to agree that using sq is worry-free and better.

Show 5 quoted lines
>>  	name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
>>  		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
>
> I trust you have tested this. But I wonder whether this leaves a stray
> newline in $re that gets in the way inside the sed expression...

I suspect the very original was written (or copied from something that wrote) like this:

	re=$(echo -n "$1" | sed -e '...')
and mechanically replaced to
	re=$(printf '%s' "$1" | sed -e '...')
because "echo" is not quite portable.

But the original misunderstands the command substitution. The trailing LF is removed by it, so as long as "$1" is a single line, $re will get a line without the trailing LF _anyway_.

Previous: Jakub Narebski
Message 9 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.