Can I set a project variable to run different build scripts among several environments?

I would like a bash environment variable which would be different on environments at build time in order to build my app with it and configure different api endpoints differently.

Only project level variables are available during build time, so you will not be able to create a variable that is different in each environment that will be accessible during builds.

You can create a project level variable using the CLI:

$ platform variable:create --level project --name NAME --value 'some-value' --json false --sensitive false --prefix 'env:' --enabled true --visible-build true --visible-runtime false --project wiqte7at4yg22

Each environment in your project will need to be redeployed in order for the changes to take affect by using the command

$ platform redeploy <environment name>

for each environment.