How to use variables with an orb?

The problem you are facing is a common issue.

The run command executes a shell, so any work done within the shell is local to the shell, so any environment variables defined, delete or modified within the shell only affect the shell’s environment and are lost once the shell returns.

This thread from a few years ago covers the issue

Passing variables to other steps - #5 by halfer

There are well-known workarounds for shell-to-shell variable passing, but I’m not sure how you would pass the variable up from the shell to the circleci environment, hopefully, someone else has that answer.