title: Different pipeline behavior between sh and ksh
body: <p>I have isolated the problem to the below code snippet:</p>
  
  <ol>
  <li>Notice below that null string gets assigned to <code>LATEST_FILE_NAME=''</code> when the script is run using <code>ksh</code>; but the script assigns the value to variable <code>$LATEST_FILE_NAME</code> correctly when run using <code>sh</code>. This in turn affects the value of <code>$FILE_LIST_COUNT</code>.</li>
  <li>But as the script is in KornShell (ksh), I am not sure what might be causing the issue.</li>
  <li>When I comment out the <code>tee</code> command in the below line, the ksh script works fine and correctly assigns the value to variable <code>$LATEST_FILE_NAME</code>.</li>
  </ol>
  
  <pre><code>(cd $SOURCE_FILE_PATH; ls *.txt 2>/dev/null) | sort -r > ${SOURCE_FILE_PATH}/${FILE_LIST} | tee -a $LOG_FILE_PATH</code></pre>
  
  <hr>
  
  <p>Kindly consider:</p>
  
  <p><strong>1. Source Code: script.sh</strong></p>
  
  <pre><code>#!/usr/bin/ksh
  set -vx # Enable debugging
  
  SCRIPTLOGSDIR=/some/path/Scripts/TEST/shell_issue
  SOURCE_FILE_PATH=/some/path/Scripts/TEST/shell_issue
  # Log file
  Timestamp=`date +%Y%m%d%H%M`
  LOG_FILENAME="TEST_LOGS_${Timestamp}.log"
  LOG_FILE_PATH="${SCRIPTLOGSDIR}/${LOG_FILENAME}"
  ## Temporary files
  FILE_LIST=FILE_LIST.temp    #Will store all  extract filenames
  FILE_LIST_COUNT=0           # Stores total number of  files
  
  getFileListDetails(){
      rm -f $SOURCE_FILE_PATH/$FILE_LIST 2&gt;&amp;1 | tee -a $LOG_FILE_PATH
  
      # Get list of all files, Sort in reverse order, and store names of the  files line-wise. If no files are found, error is muted.
      (cd $SOURCE_FILE_PATH; ls *.txt 2&gt;/dev/null) | sort -r &gt; ${SOURCE_FILE_PATH}/${FILE_LIST} | tee -a $LOG_FILE_PATH
  
      if [[ ! -f $SOURCE_FILE_PATH/$FILE_LIST ]]; then
          echo "FATAL ERROR - Could not create a temp file for  file list.";exit 1;
      fi
  
      LATEST_FILE_NAME="$(cd $SOURCE_FILE_PATH; head -1 $FILE_LIST)";
      FILE_LIST_COUNT="$(cat $SOURCE_FILE_PATH/$FILE_LIST | wc -l)";
  
  }
  
  getFileListDetails;
  exit 0;
  </code></pre>
  
  <hr>
  
  <p><strong>2. Output when using shell</strong> <code>sh script.sh</code>: </p>
  
  <pre><code>+ getFileListDetails
  + rm -f /some/path/Scripts/TEST/shell_issue/FILE_LIST.temp
  + tee -a /some/path/Scripts/TEST/shell_issue/TEST_LOGS_201304300506.log
  + cd /some/path/Scripts/TEST/shell_issue
  + sort -r
  + tee -a /some/path/Scripts/TEST/shell_issue/TEST_LOGS_201304300506.log
  + ls 1.txt 2.txt 3.txt
  + [[ ! -f /some/path/Scripts/TEST/shell_issue/FILE_LIST.temp ]]
  cd $SOURCE_FILE_PATH; head -1 $FILE_LIST
  ++ cd /some/path/Scripts/TEST/shell_issue
  ++ head -1 FILE_LIST.temp
  + LATEST_FILE_NAME=3.txt
  cat $SOURCE_FILE_PATH/$FILE_LIST | wc -l
  ++ cat /some/path/Scripts/TEST/shell_issue/FILE_LIST.temp
  ++ wc -l
  + FILE_LIST_COUNT=3
  exit 0;
  + exit 0
  </code></pre>
  
  <hr>
  
  <p><strong>3. Output when using ksh</strong> <code>ksh script.sh</code>:</p>
  
  <pre><code>+ getFileListDetails
  + tee -a /some/path/Scripts/TEST/shell_issue/TEST_LOGS_201304300507.log
  + rm -f /some/path/Scripts/TEST/shell_issue/FILE_LIST.temp
  + 2&gt;&amp; 1
  + tee -a /some/path/Scripts/TEST/shell_issue/TEST_LOGS_201304300507.log
  + sort -r
  + 1&gt; /some/path/Scripts/TEST/shell_issue/FILE_LIST.temp
  + cd /some/path/Scripts/TEST/shell_issue
  + ls 1.txt 2.txt 3.txt
  + 2&gt; /dev/null
  + [[ ! -f /some/path/Scripts/TEST/shell_issue/FILE_LIST.temp ]]
  + cd /some/path/Scripts/TEST/shell_issue
  + head -1 FILE_LIST.temp
  + LATEST_FILE_NAME=''
  + wc -l
  + cat /some/path/Scripts/TEST/shell_issue/FILE_LIST.temp
  + FILE_LIST_COUNT=0
  exit 0;+ exit 0
  </code></pre>