0

For example I have a .gitconfig for one project, since I do a lot of code reviews, that reads:

review = "!f(){ git checkout master && git pull origin master && git branch -D $1 && git fetch && git checkout $1 && git pull origin $1 && bundle install && rake db:migrate && git checkout -f; };f"

Now I know it's a little overkill and I don't need it all but I like it that way. It makes me feel like the review branch is totally clean. Sometimes it's nice to see the message Already up-to-date.

Now when the branch does not exist yet then git branch -D $1 will fail and it throws an error and doesn't run the rest of the commands.

Is there a way that I can make the rest of the commands run even if one of the commands fail?

3 Answers 3

6

That's the whole point of && - ensuring that the previous command succeeded before running the next. If you don't need that, use ; instead. The following will echo foo:

false; echo foo

And the following won't:

false && echo foo

See Bash manual, section List of Commands:

Commands separated by a ‘;’ are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

AND and OR lists are sequences of one or more pipelines separated by the control operators ‘&&’ and ‘||’, respectively. AND and OR lists are executed with left associativity.

An AND list has the form

command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero.

An OR list has the form

command1 || command2

command2 is executed if, and only if, command1 returns a non-zero exit status.

2

Yes, just replace && with ;.

The command after ; will run regardless of the exit status of the command before ;.

2
  • They will still run in succession? And only run once the last completes?
    – user974407
    Apr 18, 2016 at 12:45
  • @user974407 Yes..they will..
    – heemayl
    Apr 18, 2016 at 12:45
1

Don't use && as this means only complete this command if the previous command completed successfully.

Use ; instead, it's comparable to a new line, and is not dependent on the exit status of the previous commands.

You must log in to answer this question.

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