1

While working on a project, I often have to run these commands sequentially git add --all; git commit -m "some commit message"; git push to push the recent work to the remote repository.

I thought it would be better to write a function, that encapsulates these three commands and put that in my .zshrc file, so it is available everytime I start the shell. So I wrote the following function,

gp() {
    git add --all
    git commit -m "$*"
    git push
}

export -f gp

When I source the .zshrc file or start a new shell session. It throws the following error:

parse error near `()'

I have checked online for the correct syntax for defining function and the above function looks correct to me. I am at loss here on how to resolve this error, will greatly appreciate your help.

7
  • That export statement seems tobe taken from bash. You can't export a function like that in zsh. However, that should give you an error saying export: invalid option(s). The error that you mention seems modified. Did it not have some more text at the start of the line, like zsh: or something, or something that refers to a line in a file? The function is correct.
    – Kusalananda
    Sep 8, 2022 at 10:47
  • export -f is a bash thing. If you want to export/import functions in zsh (but really you probably don't), you'd have to add provision for that in ~/.zshenv (not recommended). Other than that, the syntax is correct (except you'd probably want to use "${(j[ ])argv}" rather than "$*" to join the arguments with space rather than whatever is the first character of $IFS and add some error handling). The error is likely caused by something further up the file or by MSDOS-style line endings. Sep 8, 2022 at 10:49
  • In any case, run info zsh function to learn how you may define a function in zsh. Sep 8, 2022 at 10:54
  • 1
    you probably don't want to do this at all, by the way; I know git add --all seems very efficient, but I can't tell you how many times beginners have started cursing themselves a week after they started doing that, when they checked in all kinds of half-done changes, temporary and irrelevant files and then had problems when these changed. So, "34 year old grumpy old supervisor/software maintainer wisdom": Whenever you want to git add --all, restrain yourself and don't. Sep 8, 2022 at 12:09
  • 2
    (this is not an exaggeration. From all the students I've advised unless they knew git before, most of them had to learn this the hard way, which then meant removing files they added with git add -a, resolving dozens of conflicts and losing hours of work time, just because someone on the internet wrote always adding all files would be a good idea. It's really not.) Sep 8, 2022 at 12:10

1 Answer 1

2
function-name() command

is one of several function definition syntaxes supported by zsh, the one from the Bourne shell from the early 80s, so that part of your code is fine.

If you get a parse error near `()' error on that, it's likely because from the start the parser is not expecting a function definition.

For instance, that's the error you'd get in:

case x in
gp() {
  body
}

As after case x in, zsh expects a case pattern or esac, not a function definition.

So the problem is likely somewhere in a part of the zshrc you're not showing. Syntax errors also often arise when scripts are formatted with MSDOS line ending (CRLF instead of LF) like when they've been edited on a Microsoft Windows systems. dos2unix can be used to fix those.

Other than that note that export -f function-name is a bash-only feature.

zsh has no equivalent to that dangerous feature, though you could implement it by hand by adding some function importing logic in your ~/.zshenv.

"$*" is also something you'd do in sh/bash as they don't have any better way to do it. That joins the positional parameters with the first character of $IFS, whatever it is at the time. In zsh, rather than leaving it up to a roll of the dice, you can specify explicitly what separator to use when joining with the j parameter expansion flag: ${(j[ ])@} for instance to join the positional parameters with spaces.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .