1

I've got the below one-liner I use to cleanup my local Git repo.

git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -D

I'm aware of its dangers but I wanted to stick it in a bash script however when I run it I get:

git_cleanup.sh: line 2: syntax error near unexpected token `('
git_cleanup.sh: line 2: `git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -D'

I thought maybe it's an extglob issue but when I check extglob from bash it shows it as off:

$ shopt extglob
extglob         off
  • Can you please tell what you want to achieve in terms of git? Like "I want to remove all branches with property X" or something like that? I'm asking because the git command branch is not meant to be used in scripts. It's a so-called porcellain command that looks nice but may look different the next day (aka release). – PerlDuck Jul 9 at 16:39
  • That error is exactly what I'd expect if bash is being run in POSIX conformant mode - do you happen to have a POSIXLY_CORRECT environment variable in effect? – steeldriver Jul 9 at 16:50
  • @steeldriver You are targeting the <(...) thing, right? That's a pure bashism, afaik. Still. There are better ways to remove branches NOT from origin (or whatever the code is supposed to do). – PerlDuck Jul 9 at 16:52
  • 1
    @PerlDuck process substitution originated in ksh I think (and is supported in ksh and zsh as well as bash). It would cause a slightly different error if run in dash (Syntax error: "(" unexpected) – steeldriver Jul 9 at 17:08
  • 1
    It looks like the OP is on a system whose /bin/sh isn't dash but bash which, when called sh, runs in POSIX mode. Leon, next time, please ask on Unix & Linux instead. – terdon Jul 9 at 17:19
1

You are running a bash script as an sh script. You haven't shown your actual script, but based on the error, I am guessing you either have an sh shebang:

#!/bin/sh

Or you are calling the script with sh instead of bash:

sh yourScript.sh

The <() operator you are using is a bash feature and isn't available in the sh shell (which is a basic shell called dash on Ubuntu systems). To fix this, just replace your shebang line with:

#!/bin/bash

Or, if you are calling the script manually, use bash:

bash yourScript.sh

To illustrate:

$ cat script.sh
wc < <(echo foo)

Now, if I run this with bash:

$ bash ./script.sh 
      1       1       4

But with sh:

$ sh ./script.sh 
./script.sh: line 1: syntax error near unexpected token `<'
./script.sh: line 1: `wc < <(echo foo)'

Actually, the error you get is not what dash would print, so I assume you aren't actually running this on an Ubuntu system. In this case, it makes no difference since the same basic issue would happen on Ubuntu as well, but next time, please ask on http://linux.stackexchange.com instead.

  • many thanks @terdon, can't believe I overlooked something so obvious, swapping the header from #!/bin/sh to #!/bin/bash resolved the issue. – Leon Roy Jul 10 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.