zsh-workers
 help / color / mirror / code / Atom feed
* Git grep command not interpreting flags correctly when an argument
@ 2019-05-19 18:09 Sam Houston
  2019-05-19 18:14 ` Roman Perepelitsa
  2019-05-19 18:32 ` Matthew Martin
  0 siblings, 2 replies; 4+ messages in thread
From: Sam Houston @ 2019-05-19 18:09 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 592 bytes --]

In a `bash` shell, in some empty directory, I can run the commands:

```bash
git init
echo "pattern" > file.txt
FLAGS="--untracked --color"
git grep $FLAGS pattern
```

And see the output:

```
file.txt:pattern
```

But in a `zsh` shell, when I run the same commands, I get the following
error:

```
error: unknown option `untracked --color'
```

The error only seems to occur when I pass in with more than one `--` in the
`FLAGS` variable.

My `zsh --version` is `zsh 5.3 (x86_64-apple-darwin18.0)`.

Let me know if I am doing something wrong, or if you need more information.

Thanks,

Sam

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Git grep command not interpreting flags correctly when an argument
  2019-05-19 18:09 Git grep command not interpreting flags correctly when an argument Sam Houston
@ 2019-05-19 18:14 ` Roman Perepelitsa
  2019-05-19 18:32 ` Matthew Martin
  1 sibling, 0 replies; 4+ messages in thread
From: Roman Perepelitsa @ 2019-05-19 18:14 UTC (permalink / raw)
  To: Sam Houston; +Cc: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1088 bytes --]

ZSH doesn't perform word splitting by default, so `git grep $FLAGS pattern`
in ZSH is equivalent to `git grep "$FLAGS" pattern` in bash. You can
explicitly request word-slitting via `$=FLAGS`. There is also an option to
do this automatically but it's not a good idea to turn it on. Most of the
time you really don't want word splitting when you type $FOO.

Roman.

On Sun, May 19, 2019 at 8:10 PM Sam Houston <sehouston3@gmail.com> wrote:

> In a `bash` shell, in some empty directory, I can run the commands:
>
> ```bash
> git init
> echo "pattern" > file.txt
> FLAGS="--untracked --color"
> git grep $FLAGS pattern
> ```
>
> And see the output:
>
> ```
> file.txt:pattern
> ```
>
> But in a `zsh` shell, when I run the same commands, I get the following
> error:
>
> ```
> error: unknown option `untracked --color'
> ```
>
> The error only seems to occur when I pass in with more than one `--` in the
> `FLAGS` variable.
>
> My `zsh --version` is `zsh 5.3 (x86_64-apple-darwin18.0)`.
>
> Let me know if I am doing something wrong, or if you need more information.
>
> Thanks,
>
> Sam
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Git grep command not interpreting flags correctly when an argument
  2019-05-19 18:09 Git grep command not interpreting flags correctly when an argument Sam Houston
  2019-05-19 18:14 ` Roman Perepelitsa
@ 2019-05-19 18:32 ` Matthew Martin
  2019-05-19 19:42   ` Sam Houston
  1 sibling, 1 reply; 4+ messages in thread
From: Matthew Martin @ 2019-05-19 18:32 UTC (permalink / raw)
  To: Sam Houston; +Cc: zsh-workers

On Sun, May 19, 2019 at 07:09:22PM +0100, Sam Houston wrote:
> In a `bash` shell, in some empty directory, I can run the commands:
> 
> ```bash
> git init
> echo "pattern" > file.txt
> FLAGS="--untracked --color"
> git grep $FLAGS pattern
> ```
> 
> And see the output:
> 
> ```
> file.txt:pattern
> ```
> 
> But in a `zsh` shell, when I run the same commands, I get the following
> error:
> 
> ```
> error: unknown option `untracked --color'
> ```

The better way (both in zsh and in bash) would be to use an array.

flags=(--untracked --color)
git grep "${flags[@]}" pattern
# git grep $flags pattern   would also work, but just in zsh

Relying on word splitting means that arguments that contain spaces or
other IFS characters are mangled into two or more arguments.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Git grep command not interpreting flags correctly when an argument
  2019-05-19 18:32 ` Matthew Martin
@ 2019-05-19 19:42   ` Sam Houston
  0 siblings, 0 replies; 4+ messages in thread
From: Sam Houston @ 2019-05-19 19:42 UTC (permalink / raw)
  To: Sam Houston, zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1022 bytes --]

Very helpful Matthew, I've changed my implementation to use a zsh array.

Thanks to you both, I've solved my problem :)

On Sun, May 19, 2019 at 7:32 PM Matthew Martin <phy1729@gmail.com> wrote:

> On Sun, May 19, 2019 at 07:09:22PM +0100, Sam Houston wrote:
> > In a `bash` shell, in some empty directory, I can run the commands:
> >
> > ```bash
> > git init
> > echo "pattern" > file.txt
> > FLAGS="--untracked --color"
> > git grep $FLAGS pattern
> > ```
> >
> > And see the output:
> >
> > ```
> > file.txt:pattern
> > ```
> >
> > But in a `zsh` shell, when I run the same commands, I get the following
> > error:
> >
> > ```
> > error: unknown option `untracked --color'
> > ```
>
> The better way (both in zsh and in bash) would be to use an array.
>
> flags=(--untracked --color)
> git grep "${flags[@]}" pattern
> # git grep $flags pattern   would also work, but just in zsh
>
> Relying on word splitting means that arguments that contain spaces or
> other IFS characters are mangled into two or more arguments.
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-05-19 19:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-19 18:09 Git grep command not interpreting flags correctly when an argument Sam Houston
2019-05-19 18:14 ` Roman Perepelitsa
2019-05-19 18:32 ` Matthew Martin
2019-05-19 19:42   ` Sam Houston

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).