Q:

Pass bash variable when sshing

I'm having some issues with multiple people running an application that requires port forwarding. I'd like to generate a random port number, ssh to the application server while passing that random number, and sed replace the default port with this one in a few different scripts. 
So is there any way I can pass a bash variable through ssh or alternatively is there a better way to accomplish this?

A:

If you know what you want to do with your variable, you can pass it to the remote host and run the commands on the same line:
ssh name@host "sed -i -e \"s/$myrandnumber/22/g\" file1 file2 file3"

$myvar will expand before the sshing happens, meaning it will be the value you set on the original machine, not the remote. You can make it a variable on the remote machine: 
ssh name@host "export remotevar=$myrandnumber; bash -i" 

But it'll only be available to the current session. If you just want the variable stored for some future date, then your best bet is writing to a file that will get sourced later, i.e.,
ssh name@host "echo \"myvar=$myrandnumber\" >> /etc/environment"

