I'm working with tmux and I'd like to pass some information from one pane to another.

For example in first pane i've got the folder by pwd command and instead of copypast to another pane I want to assign this result to some variable and use it in another one. I think I can't use bash variables because they're living in bash session or in config file.

Any idea to resolve this?


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.


There's a lightweight, fast and pretty handy tool that is used all around where data caching or interchange is needed without big efforts on coding. Redis is the name, and here it could be used like this:

  1. you start redis-server
  2. you call redis-cli setting any key to the required value in one session
  3. you call redis-cli getting the required key's value in another session

Like this:

#> redis-server &

shell session1> var_from_session1="I was set in session1"
shell session1> redis-cli set var_from_session1 "$var_from_session1"

shell session2> var_from_session1=`redis-cli get var_from_session1`
echo $var_from_session1
I was set in session1

If you know how to write traps in shell, you may create a function that's called when your target session is targeted ("killed") with a user signal. That function then would set the variables found as keys in redis to the appropriate values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.