How can I set a shell variable in a Bash script and access it later from the command line?

I’m new to Bash scripting, and I’m trying to figure out how to create a script that stores the current directory path in a variable and lets me access that variable later in the terminal.

Here’s what I’ve tried:

#!/bin/bash mypath=$(pwd) cd $1 echo $mypath exec bash

The script prints the path as expected, but when I go back to the command line and type echo $mypath, it’s empty. How can I bash set a variable in a script so that it persists and is accessible in the current shell session?

Is this even possible, or do I need to use source or some other method to make it work?