Skip to content

Instantly share code, notes, and snippets.

@ajm188
Created July 19, 2022 10:24
Show Gist options
  • Save ajm188/29e44e0a0163eb89fba8ad16521e4ca7 to your computer and use it in GitHub Desktop.
Save ajm188/29e44e0a0163eb89fba8ad16521e4ca7 to your computer and use it in GitHub Desktop.
golang exec vs exec sh
grep := exec.CommandContext(ctx,
- "grep",
- "-Er",
- `'\bflag\.[A-Z]'`,
- path,
+ "sh", "-c", strings.Join([]string{
+ "grep",
+ "-Er",
+ `'\bflag\.[A-Z]'`,
+ path,
+ }, " "),
)

Before (running grep directly in exec.Command)

2022/07/18 16:12:41 I: running /usr/bin/grep -Er \bflag\.[A-Z]" ./go/acl/
2022/07/18 16:12:41 D: dir=     path=/usr/bin/grep
2022/07/18 16:12:41 
2022/07/18 16:12:41 exit status 1
exit status 1

After (running grep via sh -c within exec.Command)

2022/07/19 06:15:41 I: running /bin/sh -c grep -Er '\bflag\.[A-Z]' ./go/acl/
2022/07/19 06:15:41 D: dir=     path=/bin/sh
go/acl/acl.go:  securityPolicy = flag.String("security_policy", "", "the name of a registered security policy to use for controlling access to URLs - empty means allow all for anyone (built-in policies: deny-all, read-only)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment