TL;DR: Instead of &&
use ;
after a command that might have errors.
My main project at work has a nice Make command for getting a new truncated version of our main database. Unfortunately, I can never remember the exact command, and it requires a second command (pg_restore
) to update my local database. It occurred to me that an alias would go a long way to making this easier for me.
alias new_db="make get_new_db && pg_restore other_args"
This was already a big win because now I don't have to wait for the database to download before executing the pg_restore
command, which also takes some time. Now I can call the alias and let it run until I have an updated database with no more input.
Recently, I also realized that we have a nice management command that resets all local passwords, so you can set a simpler password locally than for production, so I tried to add that to my alias:
alias new_db="make get_new_db && pg_restore other_args && python manage.py set_pw"
Executing new_db
still works, but the final command doesn't run. I failed to find a solution with some basic searching, but a coworker pointed out that the pg_restore
command usually has some (expected) errors. The &&
operator expects the previous command to have an exit code of 0, which this does not. In order to run a command after command with errors, you can use the ;
operator so that command becomes:
alias new_db="make get_new_db && pg_restore other_args ; python manage.py set_pw"
And now all of it runs!