title: JSch: var set in one command not visible by the next (same Session)
body: <p>I am able to successfully connect to a remote Unix host from Java using JSch and run some basic commands and view files etc.</p>
  
  <p>The problems start with setting the environment...  When i export a var in one command, it is not visible in the next as though they run in separate shells.  What I would like to do is emulate multiple commands running in the same shell.</p>
  
  <p>Here is my <strong>executeCommand</strong> method.  It takes a previously created com.jcraft.jsch.Session created by <strong>openHostSession</strong> (below):</p>
  
  <pre><code>    public static void executeCommand(Session sess, String cmd, PrintStream pstream)
      {
          System.out.println("About to execute &lt;" + cmd + "&gt;");
  
          try
          {
              ChannelExec chnl = (ChannelExec)sess.openChannel("exec");     
  
              chnl.setInputStream(null);      
  
              chnl.setErrStream(System.err);  
  
              chnl.setCommand(cmd);      
  
              InputStream in = chnl.getInputStream();      
  
              chnl.connect();      
  
              byte[] tmp=new byte[1024];      
  
              while(true)
              {        
                  while(in.available() &gt; 0)
                  {          
                      int i = in.read(tmp, 0, 1024); 
  
                      if(i &lt; 0) break;          
  
                      pstream.print(new String(tmp, 0, i));        
                  }        
  
                  if(chnl.isClosed())
                  {          
                      pstream.println("exit-status: " + chnl.getExitStatus());   
  
                      break;        
                  }        
  
                  try
                  {
                      Thread.sleep(1000);
                  }
  
                  catch(Exception ee){}      
              }      
  
              chnl.disconnect();
          }
          catch(Exception ex)
          {
              ex.printStackTrace();
          }
  
      }
  </code></pre>
  
  <p>here is <strong>openHostSession</strong>:</p>
  
  <pre><code>public static Session openHostSession(String host, String user, String passwd)
      {
          JSch jsch=new JSch();
          Session rslt = null;
  
          try
          {
              rslt = jsch.getSession(user, host, 22);
              rslt.setConfig("StrictHostKeyChecking", "no"); 
              rslt.setPassword(passwd);
              rslt.connect(30000);   
          }
          catch(JSchException jschEx)
          {
              jschEx.printStackTrace();
          }
  
          return rslt;
      }
  </code></pre>
  
  <p>client code</p>
  
  <pre><code>cmd = "export XVAR=abc";
  SshUtil.executeCommand(sess, cmd, System.out);
  
  cmd = "echo $XVAR";
  SshUtil.executeCommand(sess, cmd, System.out);
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>About to execute &lt;export XVAR=abc&gt;
  exit-status: 0
  About to execute &lt;echo $XVAR&gt;
  
  exit-status: 0
  </code></pre>
  
  <p>I can see system-level env vars, e.g.:</p>
  
  <pre><code>cmd = "echo $SHELL";
  SshUtil.executeCommand(sess, cmd, System.out);
  </code></pre>
  
  <p>returns</p>
  
  <pre><code>About to execute &lt;echo $SHELL&gt;
  /bin/ksh
  </code></pre>
  
  <p>exit-status: 0</p>
  
  <p>Similar thing happens when I source my .profile (<code>cmd = ". ./.profile";)</code>, the vars set within .profile are not visible in the next command.  The commands share the same Session but each opens its own Channel.  I tried sharing Channels but that didn't do.</p>