Remove coloured output for Terraform errors

I’m using the terraform state show command, but I’m having issues with coloured output when there is an error.

Take the following example, which fist checks the Terraform state to see if a resource is already managed by Terraform, and then imports the resource if it is not -

terraform state show module.x.y.z 2>&1
if [[ $? > 0 ]]; then
	terraform import module.x.y.z abc
else
	echo " -- Resource is already managed by Terraform."
fi

In cases where the resource is not managed by Terraform, an error is redirected to stdout. However, when this is output to the console the colour still remains, and in logs (Octopus) colour flags are added to the output and it messes it up.

Most Terraform commands seem to have the -no-color option, but not terraform state show.

Is there a more global way to get rid of the coloured output, as it’s really not wanted/required in most cases when using Terraform win CI/CD.

Thanks,
David

All Terraform commands should support the -no-color option. I can confirm that state show does, at least on Terraform 0.14:

It does seem to be missing from the docs and the help output, which is a bug.

Thanks @alisdair, that’s good to know. And you are quite correct that the option is in fact available.

I think I was probably spelling colour the proper English way (with the ‘u’) as opposed to the American way, and thus saw an error. It would be helpful if Terraform were to add an alias at some point.

Thanks,
David