<p>I am involved in the process of porting a system containing several hundreds of ksh scripts from AIX, Solaris and HPUX to Linux. I have come across the following difference in the way ksh behaves on the two systems:</p>

<pre><code>#!/bin/ksh
flag=false
echo "a\nb" | while read x
do
    flag=true
done
echo "flag = ${flag}"
exit 0
</code></pre>

<p>On AIX, Solaris and HPUX the output is "flag = true" on Linux the output is "flag = false".</p>

<p>My questions are:</p>

<ul>
<li>Is there an environment variable that I can set to get Linux's ksh to behave like the 
other Os's'? Failing that:</li>
<li>Is there an option on Linux's ksh to get the required behavior? Failing that:</li>
<li>Is there a ksh implementation available for Linux with the desired behavior?</li>
</ul>

<p>Other notes:</p>

<ul>
<li>On AIX, Solaris and HPUX ksh is a variant of ksh88.</li>
<li>On Linux, ksh is the public domain ksh (pdksh)</li>
<li>On AIX, Solaris and HPUX dtksh and ksh93 (where I have them installed) are consistent with ksh</li>
<li>The Windows NT systems I have access to: Cygwin and MKS NT, are consistent with Linux.</li>
<li>On AIX, Solaris and Linux, bash is consistent, giving the incorrect (from my perspective) result of "flag = false".</li>
</ul>

<p>The following table summarizes the systems the problem:</p>

<pre><code>uname -s       uname -r                   which ksh          ksh version                     flag =
========       ========                   =========          ===========                     ======
Linux          2.6.9-55.0.0.0.2.ELsmp     /bin/ksh           PD KSH v5.2.14 99/07/13.2       false
AIX            3                          /bin/ksh           Version M-11/16/88f             true    // AIX 5.3
                                          /bin/ksh93         Version M-12/28/93e             true
SunOS          5.8, 5.9 and 5.10          /bin/ksh           Version M-11/16/88i             true
                                          /usr/dt/bin/dtksh  Version M-12/28/93d             true
HP-UX          B.11.11 and B.11.23        /bin/ksh           Version 11/16/88                true
                                          /usr/dt/bin/dtksh  Version M-12/28/93d             true
CYGWIN_NT-5.1  1.5.25(0.156/4/2)          /bin/ksh           PD KSH v5.2.14 99/07/13.2       false
Windows_NT     5                          .../mksnt/ksh.exe  Version 8.7.0 build 1859...     false    // MKS
</code></pre>

<h1>Update</h1>

<p>After some advice from people in my company we decided to make the following modification to the code. This gives us the same result whether using the "real" ksh's (ksh88, ksh93) or any of the ksh clones (pdksh, MSK ksh). This also works correctly with bash.</p>

<pre><code>#!/bin/ksh
echo "a\nb" &gt; junk
flag=false
while read x
do
    flag=true
done &lt; junk
echo "flag = ${flag}"
exit 0
</code></pre>

<p>Thanks to jj33 for the previously accepted answer.</p>

[{'answer_id': 74421, 'author': 'zigdon', 'author_id': 4913, 'author_profile': 'https://Stackoverflow.com/users/4913', 'pm_score': 0, 'selected': False, 'text': "<p>I don't know of any particular option to force ksh to be compatible with a particular older version.  That said, perhaps you could install a very old version of ksh on your linux box, and have it behave in a compatible manner?</p>\n\n<p>It might be easier to install a more modern version of amy shell on the AIX/HP-UX boxes, and just migrate your scripts to use sh.  I know there are versions of bash available for all platforms.</p>\n"}, {'answer_id': 74446, 'author': 'jj33', 'author_id': 430, 'author_profile': 'https://Stackoverflow.com/users/430', 'pm_score': 3, 'selected': False, 'text': '<p>Instead of using pdksh on linux, use the "real" ksh from kornshell.org.  pdksh is a blind re-implementation of ksh.  kornshell.org is the original korn shell dating back 25 years or so (the one written by David Korn).  AIX and Solaris use versions of the original ksh, so the kornshell.org version is usually feature- and bug- complete.  Having cut my teeth with SunOS/Solaris, installing kornshell.org ksh is usually one of the first things I do on a new Linux box...</p>\n'}, {'answer_id': 74580, 'author': 'Alex M', 'author_id': 9652, 'author_profile': 'https://Stackoverflow.com/users/9652', 'pm_score': 0, 'selected': False, 'text': '<p>Your script gives the correct (true) output when <code>zsh</code> is used with the <code>emulate -L ksh</code> option. If all else fails you may wish to try using <code>zsh</code> on Linux.</p>\n'}, {'answer_id': 74787, 'author': 'jtimberman', 'author_id': 7672, 'author_profile': 'https://Stackoverflow.com/users/7672', 'pm_score': 1, 'selected': False, 'text': '<p>I installed \'ksh\' and \'pdksh\' on my local Ubuntu Hardy system. </p>\n\n<pre><code>ii  ksh            93s+20071105-1 The real, AT&amp;T version of the Korn shell\nii  pdksh          5.2.14-21ubunt A public domain version of the Korn shell\n</code></pre>\n\n<p>ksh has the "correct" behavior that you\'re expecting while pdksh does not. You might check your local Linux distribution\'s software repository for a "real" ksh, instead of using pdksh. The "Real Unix" OS\'s are going to install the AT&amp;T version of Korn shell, rather than pdksh, by default, what with them being based off AT&amp;T Unix (System V) :-).</p>\n'}, {'answer_id': 76858, 'author': 'szabgab', 'author_id': 11827, 'author_profile': 'https://Stackoverflow.com/users/11827', 'pm_score': 1, 'selected': False, 'text': '<p>Do you have to stay within ksh?</p>\n\n<p>Even if you use the same ksh you\'ll still call all kinds of external commands (grep, ps, cat, etc...) part of them will have different parameters and different output from system to system. Either you\'ll have to take in account those differences or use the GNU version of each one of them to make things the same.</p>\n\n<p>The <a href="http://www.perl.org/" rel="nofollow noreferrer">Perl</a> programming language originally was designed exactly to overcome this problem.\nIt includes all the features a unix shell programmer would want from he shell program but\nit is the same on every Unix system. You might not have the latest version on all those\nsystems, but if you need to install something, maybe it is better to install perl.</p>\n'}, {'answer_id': 95267, 'author': 'Andrew Stein', 'author_id': 13029, 'author_profile': 'https://Stackoverflow.com/users/13029', 'pm_score': 3, 'selected': True, 'text': '<p>After some advice from people in my company we decided to make the following modification to the code. This gives us the same result whether using the "real" ksh\'s (ksh88, ksh93) or any of the ksh clones (pdksh, MSK ksh). This also works correctly with bash.</p>\n\n<pre><code>#!/bin/ksh\necho "a\\nb" &gt; junk\nflag=false\nwhile read x\ndo\n    flag=true\ndone &lt; junk\necho "flag = ${flag}"\nexit 0\n</code></pre>\n\n<p>Thanks to jj33 for the previous accepted answer.</p>\n'}, {'answer_id': 255217, 'author': 'mpez0', 'author_id': 27898, 'author_profile': 'https://Stackoverflow.com/users/27898', 'pm_score': 1, 'selected': False, 'text': '<p>The reason for the differences is whether the inside block is executed in the original shell context or in a subshell.  You may be able to control this with the () and {} grouping commands.  Using a temporary file, as you do in your update, will work most of the time but will run into problems if the script is run twice rapidly, or if it executes without clearing the file, etc.</p>\n\n<pre><code>#!/bin/ksh\nflag=false\necho "a\\nb" | { while read x\ndo    \n     flag=true\ndone }\necho "flag = ${flag}"\nexit 0\n</code></pre>\n\n<p>That may help with the problem you were getting on the Linux ksh.  If you use parentheses instead of braces, you\'ll get the Linux behavior on the other ksh implementations.</p>\n'}, {'answer_id': 22537900, 'author': 'venkat', 'author_id': 3442785, 'author_profile': 'https://Stackoverflow.com/users/3442785', 'pm_score': 1, 'selected': False, 'text': '<p>Here is the another solution for echo "\\n" issue</p>\n\n<p><strong>Steps:</strong></p>\n\n<ol>\n<li>Find ksh package name</li>\n</ol>\n\n<p><code>$ rpm -qa --queryformat "%{NAME}-%{VERSION}-%{RELEASE}(%{ARCH})\\n" | grep "ksh"\nksh-20100621-19.el6_4.3(x86_64)</code></p>\n\n<ol>\n<li><p>uninstall ksh\n<code>$ sudo yum remove ksh-20100621-19.el6_4.3.x86_64</code></p></li>\n<li><p>down load pdksh-5.2.14-37.el5_8.1.x86_64.rpm (Please check OS for 32-bit or 64-bit and choose correct pkg)</p></li>\n<li><p>Install pdksh-5.2.14-37.el5_8.1.x86_64.rpm</p></li>\n</ol>\n\n<p><code>$ sudo yum -y install /SCRIPT_PATH/pdksh-5.2.14-37.el5_8.1.x86_64.rpm</code></p>\n\n<p><strong>Output before PDKSH install</strong></p>\n\n<pre><code>$ ora_db_start_stop.sh\n\\n==============\nUsage: START\n==============\\n\\n\n./ora_db_start_stop.sh START ALL    \\n\nOR \\n\n./ora_db_start_stop.sh START ONE_OR_MORE    \\n\n\\n==============\nUsage: STOP\n==============\\n\\n\n./ora_db_start_stop.sh STOP ALL    \\n\nOR \\n\n./ora_db_start_stop.sh STOP ONE_OR_MORE    \\n\\n\n</code></pre>\n\n<p><strong>After PDKSH install</strong></p>\n\n<p>==============</p>\n\n<h1>Usage: START</h1>\n\n<p><code>./ora_db_start_stop.sh START ALL</code></p>\n\n<p>OR</p>\n\n<p><code>./ora_db_start_stop.sh START ONE_OR_MORE</code></p>\n\n<p>==============</p>\n\n<h1>Usage: STOP</h1>\n\n<p><code>./ora_db_start_stop.sh STOP ALL</code></p>\n\n<p>OR</p>\n\n<p><code>./ora_db_start_stop.sh STOP ONE_OR_MORE</code></p>\n'}]
['https://Stackoverflow.com/questions/74372', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13029/']