Plain old temporary files:
$ pwd > /tmp/loc
$ cd "$(</tmp/loc)"
$ cd "`</tmp/loc`"
$(< file)
is similar to $(cat file)
; you can skip the double-quotes if contents don't have spaces.
If you wrap this in a function or something, change the /tmp/file
to ~/tmp/file
or even $XDG_RUNTIME_DIR/file
for some security:
put() { echo "${*:2}" > "$XDG_RUNTIME_DIR/var-$1"; }
get() { cat "$XDG_RUNTIME_DIR/var-$1"; }
forget() { rm -f "$XDG_RUNTIME_DIR/var-$1"; }
$ put somedir "$(pwd)"
$ cd "$(get somedir)"
You could get fancier and store variables in kernel keyrings via keyctl
(Linux-only):
put() { keyctl add user "$1" "${*:2}" @u; }
get() { keyctl pipe "$(keyctl search @u user "$1")"; }
forget() { keyctl unlink "$(keyctl search @u user "$1")"; }
Yes, bash variables live within the bash process itself; if you export
a variable, it's copied to that process' "environment" and copied (inherited) by stuff you run from that bash process.