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

RE: [PATCHv2] completion: make compatible with zsh

From
Peter Kjellerstedt <peter.kjellerstedt@axis.com>
Date
Aug 31, 2010, 07:52 UTC
Message-ID
<A612847CFE53224C91B23E3A5B48BAC749BF0BEBB9@xmail3.se.axis.com>
In-Reply-To
<AANLkTimNULQNVZ4hzaNY1ZobPRtj_zj545Xi29guGu4g@mail.gmail.com>
Show 48 quoted lines
> -----Original Message-----
> From: git-owner@vger.kernel.org [mailto:git-owner@vger.kernel.org] On
> Behalf Of Mark Lodato
> Sent: den 31 augusti 2010 02:56
> To: SZEDER Gábor
> Cc: Shawn O. Pearce; git@vger.kernel.org; avarab@gmail.com; Jonathan
> Nieder; Andrew Sayers
> Subject: Re: [PATCHv2] completion: make compatible with zsh
> 
> 2010/8/30 SZEDER Gábor <szeder@ira.uka.de>
> > On Thu, Aug 26, 2010 at 10:45:56PM -0400, Mark Lodato wrote:
> > > @@ -2417,3 +2433,29 @@ if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
> > >  complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
> > >       || complete -o default -o nospace -F _git git.exe
> > >  fi
> > > +
> > > +if [[ -z $ZSH_VERSION ]]; then
> >
> > -z?  I think you wanted to use -n here, like at the other places.
> 
> Oh, yes, sorry.  This was a mistake.  Thanks for catching it.
> 
> 
> > Nit: why "if [[ ... ]]"?  FWIW "if [ ... ]" would be enough.
> 
> Because you don't need to quote variables with [[ ... ]] --- e.g. [
> $lines = 0 ] fails, though in this case this feature does not matter
> --- and because [[ ... ]] is faster.
> 
> Bash 4.1.5:
> > time (for (( i = 0; i < 200000; i++ )); do [ -z $foo ]; done)
> real    0m3.430s
> user    0m3.240s
> sys     0m0.180s
> > time (for (( i = 0; i < 200000; i++ )); do [[ -z $foo ]]; done)
> real    0m2.219s
> user    0m2.090s
> sys     0m0.100s
> 
> Zsh 4.3.10:
> > time (for (( i = 0; i < 2000000; i++ )); do [ -z $foo ]; done)
> (; for ((i = 0; i < 2000000; i++ )) do; [ -z $foo ]; done; )  13.56s
> user 1.64s system 99% cpu 15.327 total
> > time (for (( i = 0; i < 2000000; i++ )); do [[ -z $foo ]]; done)
> (; for ((i = 0; i < 2000000; i++ )) do; [[ -z $foo ]]; done; )  4.62s
> user 0.01s system 99% cpu 4.644 total
> 
> Is there a reason to prefer [ ... ] ?

The [ command is defined by POSIX, while [[ is an extension to the shell language implemented by bash (and zsh). Thus the [[ construct would fail on a stricter POSIX implementation of the shell language, e.g., as implemented by dash. Thus to be POSIX compliant (which is required for shell scripts installed by a multi system supporting application like git), the [[ construct cannot be used.

Of course, this does not really apply to a script explicitly written for bash, but that is the common reason to prefer [ over [[ in shell scripts.

//Peter
Previous: Mark Lodato
Message 5 of 5 in “[PATCHv2] completion: make compatible with zsh”
  1. Mark LodatoAug 27, 2010
  2. Ævar Arnfjörð BjarmasonAug 28, 2010
  3. SZEDER GáborAug 30, 2010
  4. Mark LodatoAug 31, 2010
  5. Peter KjellerstedtAug 31, 2010

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.