1*7c356e86SAndroid Build Coastguard WorkerRCSID: $MirOS: src/bin/mksh/mksh.faq,v 1.10 2020/10/01 22:59:12 tg Exp $ 2*7c356e86SAndroid Build Coastguard WorkerToC: spelling 3*7c356e86SAndroid Build Coastguard WorkerTitle: How do you spell <tt>mksh</tt>? How do you pronounce it? 4*7c356e86SAndroid Build Coastguard Worker 5*7c356e86SAndroid Build Coastguard Worker<p>This <a href="@@RELPATH@@mksh.htm">shell</a> is spelt either 6*7c356e86SAndroid Build Coastguard Worker “<tt>mksh</tt>” (with, even at the beginning of a sentence, <a 7*7c356e86SAndroid Build Coastguard Worker href="https://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style/Capital_letters#Items_that_require_initial_lower_case">an 8*7c356e86SAndroid Build Coastguard Worker initial lowercase letter</a>; this is important) or “MirBSD Korn Shell”, 9*7c356e86SAndroid Build Coastguard Worker possibly with “the”.</p> 10*7c356e86SAndroid Build Coastguard Worker<p>I usually pronounce it as “<span xml:lang="de-DE-1901">em-ka-es-ha</span>”, 11*7c356e86SAndroid Build Coastguard Worker that is, the letters individually in my native German, or say “MirBSD Korn 12*7c356e86SAndroid Build Coastguard Worker Shell”, although it is manageable, mostly for Slavic speakers, to actually 13*7c356e86SAndroid Build Coastguard Worker say “mksh” as if it were a word ☺</p> 14*7c356e86SAndroid Build Coastguard Worker<p>Oh… I’ve run into this one, didn’t I? “MirBSD” is pronounced “<span 15*7c356e86SAndroid Build Coastguard Worker xml:lang="de-DE-1901">Mir-Be-Es-De</span>” germanically, for anglophones 16*7c356e86SAndroid Build Coastguard Worker “Mir-beas’tie” is fine.</p> 17*7c356e86SAndroid Build Coastguard Worker---- 18*7c356e86SAndroid Build Coastguard WorkerToC: sowhatismksh 19*7c356e86SAndroid Build Coastguard WorkerTitle: I’m a $OS (<i>Android, OS/2, …</i>) user, so what’s mksh? 20*7c356e86SAndroid Build Coastguard Worker 21*7c356e86SAndroid Build Coastguard Worker<p>mksh is a so-called (Unix) “shell” or “command interpreter”, similar to 22*7c356e86SAndroid Build Coastguard Worker <tt>COMMAND.COM</tt>, <tt>CMD.EXE</tt> or PowerShell on other operating 23*7c356e86SAndroid Build Coastguard Worker systems you might know. Basically, it runs in a terminal (“console” or 24*7c356e86SAndroid Build Coastguard Worker “DOS box”) window, taking user input and running that as commands. It’s 25*7c356e86SAndroid Build Coastguard Worker also used to write so-called (shell) “script”s, short programs made by 26*7c356e86SAndroid Build Coastguard Worker putting several of those commands into a “batch file”.</p> 27*7c356e86SAndroid Build Coastguard Worker<p>On Android, mksh is used as the system shell — basically, the one 28*7c356e86SAndroid Build Coastguard Worker running commands at system startup, in the background, and on user 29*7c356e86SAndroid Build Coastguard Worker behalf (but never of its own). Any privilege pop-ups you might <a 30*7c356e86SAndroid Build Coastguard Worker href="https://forum.xda-developers.com/showthread.php?t=1963976">be 31*7c356e86SAndroid Build Coastguard Worker encountering</a> are therefore <a 32*7c356e86SAndroid Build Coastguard Worker href="https://forum.xda-developers.com/showpost.php?p=33550523&postcount=1553">not 33*7c356e86SAndroid Build Coastguard Worker caused by mksh</a> but by some other code invoking mksh to do something 34*7c356e86SAndroid Build Coastguard Worker on its behalf.</p> 35*7c356e86SAndroid Build Coastguard Worker---- 36*7c356e86SAndroid Build Coastguard WorkerToC: os2 37*7c356e86SAndroid Build Coastguard WorkerTitle: I’m an OS/2 user, what else do I need to know? 38*7c356e86SAndroid Build Coastguard Worker 39*7c356e86SAndroid Build Coastguard Worker<p>Unlike the native command prompt, the current working directory is, 40*7c356e86SAndroid Build Coastguard Worker for security reasons common on Unix systems which the shell is designed 41*7c356e86SAndroid Build Coastguard Worker for, not in the search path at all; if you really need this, run the 42*7c356e86SAndroid Build Coastguard Worker command <tt>PATH=.$PATHSEP$PATH</tt> or add that to a suitable 43*7c356e86SAndroid Build Coastguard Worker initialisation file (<tt>~/.mkshrc</tt>).</p> 44*7c356e86SAndroid Build Coastguard Worker<p>There are two different newline modes for mksh-os2: standard (Unix) 45*7c356e86SAndroid Build Coastguard Worker mode, in which only LF (0A hex) is supported as line separator, and 46*7c356e86SAndroid Build Coastguard Worker “textmode”, which also accepts ASCII newlines (CR+LF), like most other 47*7c356e86SAndroid Build Coastguard Worker tools on OS/2, but creating an incompatibility with standard mksh. If 48*7c356e86SAndroid Build Coastguard Worker you compiled mksh from source, you will get the standard Unix mode unless 49*7c356e86SAndroid Build Coastguard Worker <tt>-T</tt> is added during compilation; however, you will most likely 50*7c356e86SAndroid Build Coastguard Worker have gotten this shell through komh’s port on Hobbes, or from his OS/2 51*7c356e86SAndroid Build Coastguard Worker Factory on eComStation Korea, which uses “textmode”, though. Most OS/2 52*7c356e86SAndroid Build Coastguard Worker users will want to use “textmode” unless they need absolute compatibility 53*7c356e86SAndroid Build Coastguard Worker with Unix mksh and other Unix shells and tools.</p> 54*7c356e86SAndroid Build Coastguard Worker---- 55*7c356e86SAndroid Build Coastguard WorkerToC: kornshell 56*7c356e86SAndroid Build Coastguard WorkerTitle: How does this relate to ksh or the Korn Shell? 57*7c356e86SAndroid Build Coastguard Worker 58*7c356e86SAndroid Build Coastguard Worker<p>The Korn Shell (AT&T ksh) was authored by David Korn; two major 59*7c356e86SAndroid Build Coastguard Worker flavours exist (ksh88 and ksh93), the latter having been maintained 60*7c356e86SAndroid Build Coastguard Worker until 2012 (last formal release) and 2014 (last beta snapshot, buggy). 61*7c356e86SAndroid Build Coastguard Worker A ksh86 did exist.</p> 62*7c356e86SAndroid Build Coastguard Worker<p>There’s now <tt>ksh2020</tt>, a project having restarted development 63*7c356e86SAndroid Build Coastguard Worker around November 2017 forking the last <tt>ksh93 v-</tt> (beta) snapshot 64*7c356e86SAndroid Build Coastguard Worker and continuing to develop it, presented at FOSDEM.</p> 65*7c356e86SAndroid Build Coastguard Worker<p>AT&T ksh88 is “the (original) Korn Shell”. Other implementations, 66*7c356e86SAndroid Build Coastguard Worker of varying quality (MKS Toolkit’s MKS ksh being named as an example of 67*7c356e86SAndroid Build Coastguard Worker the lower end, MirBSD’s mksh at the upper end). They are all <em>not</em> 68*7c356e86SAndroid Build Coastguard Worker “Korn Shell” or “ksh”. However, mksh got blessed by David Korn, as long 69*7c356e86SAndroid Build Coastguard Worker as it cannot be confused with the original Korn Shell.</p> 70*7c356e86SAndroid Build Coastguard Worker<p>The POSIX shell standard, while lacking most Korn Shell features, was 71*7c356e86SAndroid Build Coastguard Worker largely based on AT&T ksh88, with some from the Bourne shell.</p> 72*7c356e86SAndroid Build Coastguard Worker<p>mksh is the currently active development of what started as the Public 73*7c356e86SAndroid Build Coastguard Worker Domain Bourne Shell in the mid-1980s with ksh88-compatibl-ish extensions 74*7c356e86SAndroid Build Coastguard Worker having been added later, making the Public Domain Korn Shell (pdksh), 75*7c356e86SAndroid Build Coastguard Worker which, while never officially blessed, was the only way for most to get 76*7c356e86SAndroid Build Coastguard Worker a Korn Shell-like command interpreter for AT&T’s was proprietary, 77*7c356e86SAndroid Build Coastguard Worker closed-source code for a very long time. pdksh’s development ended in 78*7c356e86SAndroid Build Coastguard Worker 1999, with some projects like Debian and NetBSD® creating small bug fixes 79*7c356e86SAndroid Build Coastguard Worker (which often introduced new bugs) as part of maintenance. Around 2003, 80*7c356e86SAndroid Build Coastguard Worker OpenBSD started cleaning up their shipped version of pdksh, removing old 81*7c356e86SAndroid Build Coastguard Worker and compatibility code and modernising it. In 2002, development of what 82*7c356e86SAndroid Build Coastguard Worker is now mksh started as the system shell of MirBSD, which took over almost 83*7c356e86SAndroid Build Coastguard Worker all of OpenBSD’s cleanup, adding compatibility to other operating systems 84*7c356e86SAndroid Build Coastguard Worker back on top of it, and after 2004, independent, massive development of 85*7c356e86SAndroid Build Coastguard Worker bugfixes including a complete reorganisation of the way the parser works, 86*7c356e86SAndroid Build Coastguard Worker and of new features both independent and compatible with other shells 87*7c356e86SAndroid Build Coastguard Worker (ksh93, GNU bash, zsh, BSD csh) started and was followed by working with 88*7c356e86SAndroid Build Coastguard Worker the group behind POSIX to fix issues both in the standard and in mksh. 89*7c356e86SAndroid Build Coastguard Worker mksh became the system shell in several other operating systems and Linux 90*7c356e86SAndroid Build Coastguard Worker distributions and Android and thus is likely the Korn shell, if not Unix 91*7c356e86SAndroid Build Coastguard Worker shell, flavour with the largest user base. It has replaced pdksh in all 92*7c356e86SAndroid Build Coastguard Worker contemporary systems except QNX, NetBSD® and OpenBSD (who continue to 93*7c356e86SAndroid Build Coastguard Worker maintain their variant on “low flame”).</p> 94*7c356e86SAndroid Build Coastguard Worker<p>dtksh is the “Desktop Korn Shell”, a build of AT&T ksh93 with some 95*7c356e86SAndroid Build Coastguard Worker additional built-in utilities for graphics programming (windows, menu 96*7c356e86SAndroid Build Coastguard Worker bars, dialogue boxes, etc.) utilising Motif bindings.</p> 97*7c356e86SAndroid Build Coastguard Worker<p>MKS ksh is a proprietary reimplemention aiming for, but not quite 98*7c356e86SAndroid Build Coastguard Worker getting close to, ksh88 compatibility.</p> 99*7c356e86SAndroid Build Coastguard Worker<p>SKsh is an AmigaOS-specific Korn Shell-lookalike by Steve Koren.</p> 100*7c356e86SAndroid Build Coastguard Worker<p>The <a href="@@RELPATH@@ksh-chan.htm">Homepage of the <tt>#ksh</tt> 101*7c356e86SAndroid Build Coastguard Worker channel on Freenode IRC</a> contains more information about the Korn 102*7c356e86SAndroid Build Coastguard Worker Shell in general and its flavours.</p> 103*7c356e86SAndroid Build Coastguard Worker---- 104*7c356e86SAndroid Build Coastguard WorkerToC: packaging 105*7c356e86SAndroid Build Coastguard WorkerTitle: How should I package mksh? (common cases) 106*7c356e86SAndroid Build Coastguard Worker 107*7c356e86SAndroid Build Coastguard Worker<p>Export a few environment variables, namely <tt>CC</tt> (the C compiler), 108*7c356e86SAndroid Build Coastguard Worker <tt>CPPFLAGS</tt> (all C præprocessor definitions), <tt>CFLAGS</tt> (only 109*7c356e86SAndroid Build Coastguard Worker compiler flags, <em>no</em> <tt>-Dfoo</tt> or anything!), <tt>LDFLAGS</tt> 110*7c356e86SAndroid Build Coastguard Worker (for anything to pass to the C compiler while linking) and <tt>LIBS</tt> 111*7c356e86SAndroid Build Coastguard Worker (appended to the linking command line after everything else. You might 112*7c356e86SAndroid Build Coastguard Worker wish to <tt>export LDSTATIC=-static</tt> for a static build as well.</p> 113*7c356e86SAndroid Build Coastguard Worker<p>When cross-compiling, <tt>CC</tt> is the <em>cross</em> compiler (mksh 114*7c356e86SAndroid Build Coastguard Worker currently does not require a compiler targetting the build system), but 115*7c356e86SAndroid Build Coastguard Worker you <em>must</em> also export <tt>TARGET_OS</tt> to whatever system you 116*7c356e86SAndroid Build Coastguard Worker are compiling for, e.g. “Linux”. For most operating systems, that’s just 117*7c356e86SAndroid Build Coastguard Worker the uname(1) output. Some very rare systems also need <tt>TARGET_OSREV</tt>; 118*7c356e86SAndroid Build Coastguard Worker consult the source code of <tt>Build.sh</tt> for details.</p> 119*7c356e86SAndroid Build Coastguard Worker<p>Create two subdirectories, say <tt>build-mksh</tt> and <tt>build-lksh</tt>. 120*7c356e86SAndroid Build Coastguard Worker In each of them, start a compilation by issuing <tt>sh ../Build.sh -r</tt> 121*7c356e86SAndroid Build Coastguard Worker followed by running the testsuite<a href="#packaging-fn1">¹</a> via 122*7c356e86SAndroid Build Coastguard Worker <tt>./test.sh</tt>. For lksh(1) add <tt>-DMKSH_BINSHPOSIX</tt> to 123*7c356e86SAndroid Build Coastguard Worker <tt>CPPFLAGS</tt> and use <tt>sh ../Build.sh -r -L</tt> to compile.</p> 124*7c356e86SAndroid Build Coastguard Worker<p>See <a href="#testsuite-fails">below</a> if the testsuite fails.</p> 125*7c356e86SAndroid Build Coastguard Worker<p>Install <tt>build-mksh/mksh</tt> as <tt>/bin/mksh</tt> (or similar), 126*7c356e86SAndroid Build Coastguard Worker <tt>build-lksh/lksh</tt> as <tt>/bin/lksh</tt> with a symlink(7) to it 127*7c356e86SAndroid Build Coastguard Worker from <tt>/bin/sh</tt> (if desred), and <tt>mksh.1</tt> and <tt>lksh.1</tt> 128*7c356e86SAndroid Build Coastguard Worker as manpages (mdoc macropackage required). Install <tt>dot.mkshrc</tt> 129*7c356e86SAndroid Build Coastguard Worker either as <tt>/etc/skel/.mkshrc</tt> (meaning your users will have to 130*7c356e86SAndroid Build Coastguard Worker manually resynchronise their home directories’ copies after every package 131*7c356e86SAndroid Build Coastguard Worker upgrade) or as <tt>/etc/mkshrc</tt>, in which case you install a <a 132*7c356e86SAndroid Build Coastguard Worker href="https://evolvis.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=alioth/mksh.git;a=blob;f=debian/.mkshrc;hb=HEAD">redirection 133*7c356e86SAndroid Build Coastguard Worker script like Debian’s</a> into <tt>/etc/skel/.mkshrc</tt>. You may need a <a 134*7c356e86SAndroid Build Coastguard Worker href="@@RELPATH@@TaC-mksh.txt">summary of the licence information</a>.</p> 135*7c356e86SAndroid Build Coastguard Worker<p>At runtime, the presence of <tt>/bin/ed</tt> as default history editor 136*7c356e86SAndroid Build Coastguard Worker is recommended, as well as a manpage formatter; you can also install 137*7c356e86SAndroid Build Coastguard Worker preformatted manpages from <tt>build-*ksh/*ksh.cat1</tt> if nroff(1) (or 138*7c356e86SAndroid Build Coastguard Worker <tt>$NROFF</tt>) is available at build time by removing the <tt>-r</tt> 139*7c356e86SAndroid Build Coastguard Worker flag from either <tt>Build.sh</tt> invocation.</p> 140*7c356e86SAndroid Build Coastguard Worker<p>Some shell features require the ability to create temporary files and 141*7c356e86SAndroid Build Coastguard Worker FIFOS (cf. mkfifo(2))/pipes at runtime. Set <tt>TMPDIR</tt> to a suitable 142*7c356e86SAndroid Build Coastguard Worker location if <tt>/tmp</tt> isn’t it; if this is known ahead of time, you 143*7c356e86SAndroid Build Coastguard Worker can add <tt>-DMKSH_DEFAULT_TMPDIR=\"/path/to/tmp\"</tt> to CPPFLAGS. We 144*7c356e86SAndroid Build Coastguard Worker currently are unable to determine one on Android because its bionic libc 145*7c356e86SAndroid Build Coastguard Worker does not expose any method suitable to do so in the generic case.</p> 146*7c356e86SAndroid Build Coastguard Worker<p id="packaging-fn1">① To run the testsuite, ed(1) must be available as 147*7c356e86SAndroid Build Coastguard Worker <tt>/bin/ed</tt>, and perl(1) is needed. When cross-compiling, the version 148*7c356e86SAndroid Build Coastguard Worker of the first <tt>ed</tt> binary on the <tt>PATH</tt> <em>must</em> be the 149*7c356e86SAndroid Build Coastguard Worker same as that in the target system on which the tests are to be run, in 150*7c356e86SAndroid Build Coastguard Worker order to be able to detect which flavour of ed to adjust the tests for. 151*7c356e86SAndroid Build Coastguard Worker Busybox ed is broken beyond repair, and all three ed-related tests will 152*7c356e86SAndroid Build Coastguard Worker always fail with it.</p> 153*7c356e86SAndroid Build Coastguard Worker---- 154*7c356e86SAndroid Build Coastguard WorkerToC: mkshrc 155*7c356e86SAndroid Build Coastguard WorkerTitle: How does mksh load configuration files? 156*7c356e86SAndroid Build Coastguard Worker 157*7c356e86SAndroid Build Coastguard Worker<p>The shell loads first <tt>/etc/profile</tt> then <tt>~/.profile</tt> 158*7c356e86SAndroid Build Coastguard Worker if called as login shell or with the <tt>-l</tt> flag, then loads the file 159*7c356e86SAndroid Build Coastguard Worker <tt>$ENV</tt> points to (defaulting to <tt>~/.mkshrc</tt>) for interactive 160*7c356e86SAndroid Build Coastguard Worker shells (that includes login shells).</p> 161*7c356e86SAndroid Build Coastguard Worker<p>Distributors should take care to either install the <tt>dot.mkshrc</tt> 162*7c356e86SAndroid Build Coastguard Worker example provided into <tt>/etc/skel/.mkshrc</tt> (so that it’s available 163*7c356e86SAndroid Build Coastguard Worker for newly created user accounts) and ensure it can propagate to existing 164*7c356e86SAndroid Build Coastguard Worker accounts or, if upgrading these is difficult, install the shipped file 165*7c356e86SAndroid Build Coastguard Worker as, for example, <tt>/etc/mkshrc</tt> and install a skeleton file, such 166*7c356e86SAndroid Build Coastguard Worker as the one in Debian, that sources the file in <tt>/etc</tt>.</p> 167*7c356e86SAndroid Build Coastguard Worker<p>It’s vital that users can change the configuration, so do not force a 168*7c356e86SAndroid Build Coastguard Worker root-provided config file onto them; the shipped file, after all, is just 169*7c356e86SAndroid Build Coastguard Worker an example.</p> 170*7c356e86SAndroid Build Coastguard Worker<p>If you need central user and configuration management and cannot use 171*7c356e86SAndroid Build Coastguard Worker something that installs skeleton files upon home directory creation 172*7c356e86SAndroid Build Coastguard Worker (like pam_mkhomedir), you can <tt>export ENV</tt> in <tt>/etc/profile</tt> 173*7c356e86SAndroid Build Coastguard Worker to a file (say <tt>/etc/shellrc</tt>) that sources the per-shell file. 174*7c356e86SAndroid Build Coastguard Worker Users can, this way, still override it by setting a different <tt>$ENV</tt> 175*7c356e86SAndroid Build Coastguard Worker in their <tt>~/.profile</tt>.</p> 176*7c356e86SAndroid Build Coastguard Worker---- 177*7c356e86SAndroid Build Coastguard WorkerToC: testsuite-fails 178*7c356e86SAndroid Build Coastguard WorkerTitle: The testsuite fails! 179*7c356e86SAndroid Build Coastguard Worker 180*7c356e86SAndroid Build Coastguard Worker<p>The mksh testsuite has uncovered numerous bugs in operating systems 181*7c356e86SAndroid Build Coastguard Worker (kernels, libraries), compilers and toolchains. It is likely that you 182*7c356e86SAndroid Build Coastguard Worker just ran into one. If you’re using LTO (the <tt>Build.sh</tt> option 183*7c356e86SAndroid Build Coastguard Worker <tt>-c lto</tt>) try to disable it first — especially GCC is a repeat 184*7c356e86SAndroid Build Coastguard Worker offender breaking LTO and its antecessor <tt>-fwhole-program --combine</tt> 185*7c356e86SAndroid Build Coastguard Worker and tends to do wrong code generation quite a bit. Otherwise, try 186*7c356e86SAndroid Build Coastguard Worker lowering the optimisation levels, bisecting, etc.</p> 187*7c356e86SAndroid Build Coastguard Worker---- 188*7c356e86SAndroid Build Coastguard WorkerToC: selinux-androidiocy 189*7c356e86SAndroid Build Coastguard WorkerTitle: I forbid stat(2) in my SELinux policy, and some things do not work! 190*7c356e86SAndroid Build Coastguard Worker 191*7c356e86SAndroid Build Coastguard WorkerDon’t break Unix. Read up on the GIGO principle. Duh. 192*7c356e86SAndroid Build Coastguard Worker---- 193*7c356e86SAndroid Build Coastguard WorkerToC: makefile 194*7c356e86SAndroid Build Coastguard WorkerTitle: Why doesn’t this use a Makefile to build? 195*7c356e86SAndroid Build Coastguard Worker 196*7c356e86SAndroid Build Coastguard Worker<p>Not all supported target operating environments have a make utility 197*7c356e86SAndroid Build Coastguard Worker available, and shell was required for “mirtoconf” (like autoconf) 198*7c356e86SAndroid Build Coastguard Worker already anyway, so it was chosen to run the make part as well.</p> 199*7c356e86SAndroid Build Coastguard Worker<p>You can, however, add the <tt>-M</tt> flag to your <tt>Build.sh</tt> 200*7c356e86SAndroid Build Coastguard Worker invocations to let it produce a <tt>Makefrag.inc</tt> file <em>tailored 201*7c356e86SAndroid Build Coastguard Worker for this specific build</em> which you can then include in a Makefile, 202*7c356e86SAndroid Build Coastguard Worker such as with the BSD make(1) “.include” command or <a 203*7c356e86SAndroid Build Coastguard Worker href="https://www.gnu.org/software/make/manual/make.html#Include">GNU 204*7c356e86SAndroid Build Coastguard Worker make</a> equivalent. It even contains, for the user to start out with, 205*7c356e86SAndroid Build Coastguard Worker a commented-out example of how to do that in the most basic manner.</p> 206*7c356e86SAndroid Build Coastguard Worker---- 207*7c356e86SAndroid Build Coastguard WorkerToC: oldbsd 208*7c356e86SAndroid Build Coastguard WorkerTitle: Why do other BSDs and QNX still use pdksh instead of mksh? 209*7c356e86SAndroid Build Coastguard Worker 210*7c356e86SAndroid Build Coastguard Worker<p>Some systems are resistant to change, mostly due to bikeshedding 211*7c356e86SAndroid Build Coastguard Worker (some people would, for example, rather see all shells banned to 212*7c356e86SAndroid Build Coastguard Worker ports/pkgsrc®) and hysterial raisins (historical reasons ☻). Most 213*7c356e86SAndroid Build Coastguard Worker BSDs have mksh packages available, and it works on all of them and 214*7c356e86SAndroid Build Coastguard Worker QNX just fine.</p> 215*7c356e86SAndroid Build Coastguard Worker<p>In fact, on all of these systems, you can replace their 1999-era 216*7c356e86SAndroid Build Coastguard Worker <tt>/bin/ksh</tt> (which is a pdksh) with mksh. On at least NetBSD® 217*7c356e86SAndroid Build Coastguard Worker 1.6 and up (not 1.5) and OpenBSD, even <tt>/bin/sh</tt> is fair game.</p> 218*7c356e86SAndroid Build Coastguard Worker<p>MidnightBSD notably has adopted mksh as system shell (thanks laffer1).</p> 219*7c356e86SAndroid Build Coastguard Worker---- 220*7c356e86SAndroid Build Coastguard WorkerToC: openbsd 221*7c356e86SAndroid Build Coastguard WorkerTitle: Why is there no mksh in OpenBSD’s ports tree? 222*7c356e86SAndroid Build Coastguard Worker 223*7c356e86SAndroid Build Coastguard WorkerOpenBSD don’t like people who fork off their project at all; heck, 224*7c356e86SAndroid Build Coastguard Workerthey don’t even like the people they themselves forked off (NetBSD®). 225*7c356e86SAndroid Build Coastguard WorkerSeveral people tried over the years to get one committed, but nobody 226*7c356e86SAndroid Build Coastguard Workerdared so as to not lose their commit bit. If you try, succeed, and 227*7c356e86SAndroid Build Coastguard Workersurvive Theo, however, kudos to you! See also <a href="#oldbsd">the 228*7c356e86SAndroid Build Coastguard Worker“other BSDs” FAQ entry</a>. 229*7c356e86SAndroid Build Coastguard Worker---- 230*7c356e86SAndroid Build Coastguard WorkerToC: book 231*7c356e86SAndroid Build Coastguard WorkerTitle: I’d like an introduction. 232*7c356e86SAndroid Build Coastguard Worker 233*7c356e86SAndroid Build Coastguard WorkerUnfortunately, nobody has written a book about mksh yet, although 234*7c356e86SAndroid Build Coastguard Workerother shells have received (sometimes decent) attention from authors 235*7c356e86SAndroid Build Coastguard Workerand publishers. This FAQ lists a subset of things packagers and 236*7c356e86SAndroid Build Coastguard Workergeneric people ask, and the mksh(1) manpage is more of a reference, 237*7c356e86SAndroid Build Coastguard Workerso you are probably best off starting with a shell-agnostic, POSIX 238*7c356e86SAndroid Build Coastguard Workeror ksh88 reference such as the first edition (the second one deals 239*7c356e86SAndroid Build Coastguard Workerwith ksh93 which differs far more from mksh than ksh88, as ancient 240*7c356e86SAndroid Build Coastguard Workeras it is, does) of the O’Reilly book (⚠ disclaimer: only an example, 241*7c356e86SAndroid Build Coastguard Workernot a recommendation) and going forward by reading scripts (the 242*7c356e86SAndroid Build Coastguard Worker“shellsnippets” repository referenced in the <tt>#ksh</tt> channel 243*7c356e86SAndroid Build Coastguard Workerhomepage (see the top of this document) has many examples) and 244*7c356e86SAndroid Build Coastguard Workertrying to understand them and the mksh specifics from the manpage. 245*7c356e86SAndroid Build Coastguard Worker---- 246*7c356e86SAndroid Build Coastguard WorkerToC: ps1conv 247*7c356e86SAndroid Build Coastguard WorkerTitle: My prompt from <<i>some other shell</i>> does not work! 248*7c356e86SAndroid Build Coastguard Worker 249*7c356e86SAndroid Build Coastguard Worker<a href="#contact">Contact</a> us on the mailing list or on IRC, 250*7c356e86SAndroid Build Coastguard Workerwe’ll convert it for you. Also have a look at the PS1 section in 251*7c356e86SAndroid Build Coastguard Workerthe mksh(1) manpage (search for “otherwise unused char”, e.g. with 252*7c356e86SAndroid Build Coastguard Worker<tt>/</tt> in less(1), to spot it quickly). 253*7c356e86SAndroid Build Coastguard Worker---- 254*7c356e86SAndroid Build Coastguard WorkerToC: ps1weird 255*7c356e86SAndroid Build Coastguard WorkerTitle: My prompt is weird! 256*7c356e86SAndroid Build Coastguard Worker 257*7c356e86SAndroid Build Coastguard Worker<p>There are several reasons why your <tt>PS1</tt> might be not 258*7c356e86SAndroid Build Coastguard Worker what you’d expect:</p><ul> 259*7c356e86SAndroid Build Coastguard Worker<li><tt>$PS1</tt> is <tt>export</tt>ed. <strong>Do not export PS1!</strong> 260*7c356e86SAndroid Build Coastguard Worker (This was agreed upon as suggestion in a discussion between bash, zsh and 261*7c356e86SAndroid Build Coastguard Worker Korn shell developers.) The feature set of different shells vastly differs 262*7c356e86SAndroid Build Coastguard Worker and each shell should use its default PS1 or from its startup files.</li> 263*7c356e86SAndroid Build Coastguard Worker<li><tt>$ENV</tt> <a href="#env">is set and/or <tt>export</tt>ed</a>.</li> 264*7c356e86SAndroid Build Coastguard Worker<li>Your prompt is just “<tt># </tt>”: you’re entering a root shell, and 265*7c356e86SAndroid Build Coastguard Worker <tt>$PS1</tt> does not contain the ‘#’ character, in which case the shell 266*7c356e86SAndroid Build Coastguard Worker forces this prompt, making extra privileges obvious.</li> 267*7c356e86SAndroid Build Coastguard Worker<li>Your prompt is just “<tt>$ </tt>”: perhaps your system administrator 268*7c356e86SAndroid Build Coastguard Worker did not install the shipped <tt>dot.mkshrc</tt> file, or you did not copy 269*7c356e86SAndroid Build Coastguard Worker <tt>/etc/skel/.mkshrc</tt> into your home directory (perhaps it was created 270*7c356e86SAndroid Build Coastguard Worker before <tt>mksh</tt> was installed?). Without another idea for a fix, get <a 271*7c356e86SAndroid Build Coastguard Worker href="http://www.mirbsd.org/cvs.cgi/~checkout~/src/bin/mksh/dot.mkshrc?rev=HEAD;content-type=text%2Fplain">this 272*7c356e86SAndroid Build Coastguard Worker file</a> and store it as <tt>~/.mkshrc</tt> then run <tt>mksh</tt>; this 273*7c356e86SAndroid Build Coastguard Worker will at the very least install our sample (“user@host:path $ ”) prompt.</li> 274*7c356e86SAndroid Build Coastguard Worker<li>Your prompt contains things like “\u” or “\w”: it is for another shell 275*7c356e86SAndroid Build Coastguard Worker and <a href="#ps1conv">needs converting</a>.</li> 276*7c356e86SAndroid Build Coastguard Worker<li>Your prompt contains colours, and when the command line is long the 277*7c356e86SAndroid Build Coastguard Worker cursor position or screen contents, especially using the history, is off: 278*7c356e86SAndroid Build Coastguard Worker terminal escapes must be escaped from the shell; check the PS1 section in 279*7c356e86SAndroid Build Coastguard Worker the manpage: search for “otherwise unused char” (see above).</li> 280*7c356e86SAndroid Build Coastguard Worker<li>If the prompt doesn’t leave enough space on the right, the shell inserts 281*7c356e86SAndroid Build Coastguard Worker a line break after it when rendering.</li> 282*7c356e86SAndroid Build Coastguard Worker</ul> 283*7c356e86SAndroid Build Coastguard Worker---- 284*7c356e86SAndroid Build Coastguard WorkerToC: env 285*7c356e86SAndroid Build Coastguard WorkerTitle: On startup files and <tt>$ENV</tt> across and detecting various shells 286*7c356e86SAndroid Build Coastguard Worker 287*7c356e86SAndroid Build Coastguard WorkerInteractive shells look at <tt>~/.mkshrc</tt> (or <tt>/system/etc/mkshrc</tt> 288*7c356e86SAndroid Build Coastguard Workeron Android and <tt>/etc/mkshrc</tt> on FreeWRT and OpenWrt) by default. This 289*7c356e86SAndroid Build Coastguard Workerlocation can, however, be overridden by setting the <tt>ENV</tt> environment 290*7c356e86SAndroid Build Coastguard Workervariable. (FreeBSD is rumoured to set it in their system profile.) It’s better 291*7c356e86SAndroid Build Coastguard Workerto not set <tt>$ENV</tt> if possible and let every shell user their native 292*7c356e86SAndroid Build Coastguard Workerstartup files; otherwise, you must ensure that it runs under all shells. Check 293*7c356e86SAndroid Build Coastguard Worker<tt>$BASH_VERSION</tt> (GNU bash), <tt>$KSH_VERSION</tt> (contains “LEGACY KSH” 294*7c356e86SAndroid Build Coastguard Workeror “MIRBSD KSH” for mksh, “PD KSH” for ancient mirbsdksh/oksh/pdksh, “Version” 295*7c356e86SAndroid Build Coastguard Workerfor ksh93); <tt>$NETBSD_SHELL</tt> (NetBSD ash); <tt>POSH_VERSION</tt> (posh, a 296*7c356e86SAndroid Build Coastguard Workerpdksh derivative); <tt>$SH_VERSION</tt> (“PD KSH” as sh), <tt>$YASH_VERSION</tt> 297*7c356e86SAndroid Build Coastguard Worker(yash), <tt>$ZSH_VERSION</tt> (or if <tt>$VERSION</tt> begins with “zsh”); a <a 298*7c356e86SAndroid Build Coastguard Workerhref="@@RELPATH@@ksh-chan.htm#which-shell">list of more approaches</a> exists. 299*7c356e86SAndroid Build Coastguard Worker---- 300*7c356e86SAndroid Build Coastguard WorkerToC: ctrl-x-e 301*7c356e86SAndroid Build Coastguard WorkerTitle: Multiline command editing 302*7c356e86SAndroid Build Coastguard Worker 303*7c356e86SAndroid Build Coastguard Worker<p>mksh is very independent of the terminal and external libraries and 304*7c356e86SAndroid Build Coastguard Worker databases, such as termcap, and therefore is conservative in which ANSI 305*7c356e86SAndroid Build Coastguard Worker control codes are sent to the terminal.</p> 306*7c356e86SAndroid Build Coastguard Worker<p>For this reason, mksh’s input line editing uses a “windowed one-line” 307*7c356e86SAndroid Build Coastguard Worker concept: the line the cursor is on is a “window” into the whole input, 308*7c356e86SAndroid Build Coastguard Worker horizontally scrolled. Some other shells (that are much larger and have 309*7c356e86SAndroid Build Coastguard Worker more dependencies on external tooling) use a “multi-line” editing mode, 310*7c356e86SAndroid Build Coastguard Worker and users occasionally wish for this. It is on the long-term TODO, but 311*7c356e86SAndroid Build Coastguard Worker (due to the aforementioned implications) this is not trivial.</p> 312*7c356e86SAndroid Build Coastguard Worker<p>One way to achieve multi-line editing is to <em>dis</em>able input 313*7c356e86SAndroid Build Coastguard Worker line editing: <tt>set +o emacs +o vi</tt><br />This will, however, lose 314*7c356e86SAndroid Build Coastguard Worker you all editing features: tab completion, cursor keys, history, etc.</p> 315*7c356e86SAndroid Build Coastguard Worker<p>Another way, if you don’t need it all the time, is to use a function 316*7c356e86SAndroid Build Coastguard Worker that spawns your editor on the input line: press <tt>^Xe</tt> in the 317*7c356e86SAndroid Build Coastguard Worker default emacs mode or <tt>Esc + v</tt> in vi mode. Once you exit the 318*7c356e86SAndroid Build Coastguard Worker editor, whatever was written there is run; this includes the original 319*7c356e86SAndroid Build Coastguard Worker command line if you quit without saving, so request the editor to exit 320*7c356e86SAndroid Build Coastguard Worker nōn-zero (e.g. using jupp’s “abendjoe” command) to prevent execution. 321*7c356e86SAndroid Build Coastguard Worker This is <em>really</em> useful to write ad-hōc scripts as well.</p> 322*7c356e86SAndroid Build Coastguard Worker---- 323*7c356e86SAndroid Build Coastguard WorkerToC: ctrl-l-cls 324*7c356e86SAndroid Build Coastguard WorkerTitle: ^L (Ctrl-L) does not clear the screen 325*7c356e86SAndroid Build Coastguard Worker 326*7c356e86SAndroid Build Coastguard WorkerUse ^[^L (Escape+Ctrl-L) or rebind it:<br /> 327*7c356e86SAndroid Build Coastguard Worker<tt>bind '^L=clear-screen'</tt> 328*7c356e86SAndroid Build Coastguard Worker---- 329*7c356e86SAndroid Build Coastguard WorkerToC: ctrl-u-pico 330*7c356e86SAndroid Build Coastguard WorkerTitle: ^U (Ctrl-U) clears the entire line 331*7c356e86SAndroid Build Coastguard Worker 332*7c356e86SAndroid Build Coastguard WorkerIf it should only delete the line up to the cursor, use:<br /> 333*7c356e86SAndroid Build Coastguard Worker<tt>bind -m ^U='^[0^K'</tt> 334*7c356e86SAndroid Build Coastguard Worker---- 335*7c356e86SAndroid Build Coastguard WorkerToC: cur-up-zsh 336*7c356e86SAndroid Build Coastguard WorkerTitle: Cursor Up behaves differently from zsh 337*7c356e86SAndroid Build Coastguard Worker 338*7c356e86SAndroid Build Coastguard WorkerSome shells make Cursor Up search in the history only for commands 339*7c356e86SAndroid Build Coastguard Workerstarting with what was already entered. mksh separates the shortcuts: 340*7c356e86SAndroid Build Coastguard WorkerCursor Up goes up one command and PgUp searches the history as described 341*7c356e86SAndroid Build Coastguard Workerabove. You can, of course, rebind:<br /> 342*7c356e86SAndroid Build Coastguard Worker<tt>bind '^XA=search-history-up'</tt> 343*7c356e86SAndroid Build Coastguard Worker---- 344*7c356e86SAndroid Build Coastguard WorkerToC: current 345*7c356e86SAndroid Build Coastguard WorkerTitle: Can mksh set the title of the window according to the command running? 346*7c356e86SAndroid Build Coastguard Worker 347*7c356e86SAndroid Build Coastguard WorkerThere’s no such thing as “the command currently running”; consider 348*7c356e86SAndroid Build Coastguard Workerpipelines and delays (<tt>cmd1 | (cmd2; sleep 3; cmd3) | cmd4</tt>). 349*7c356e86SAndroid Build Coastguard WorkerThere is, however, a way to make the shell display the command <em>line</em> 350*7c356e86SAndroid Build Coastguard Workerduring the time it is executed; for testing, you will need to download <a 351*7c356e86SAndroid Build Coastguard Workerhref="https://evolvis.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=shellsnippets/shellsnippets.git;a=blob;f=mksh/terminal-title;hb=HEAD">this 352*7c356e86SAndroid Build Coastguard Workerscript</a> and <tt>source</tt> it. For merging into your <tt>~/.mkshrc</tt> 353*7c356e86SAndroid Build Coastguard Workeryou should first understand how it works: lines 4–18 set a <tt>PS1</tt> 354*7c356e86SAndroid Build Coastguard Worker(prompt) equivalent to lines 84–96 of the stock <tt>dot.mkshrc</tt>, with 355*7c356e86SAndroid Build Coastguard Workerone change: line 15 (<tt>print >/dev/tty …</tt>) is new, inserted just 356*7c356e86SAndroid Build Coastguard Workerbefore the <tt>return</tt> command of the function substitution in the 357*7c356e86SAndroid Build Coastguard Workerdefault prompt; this is what you’ll need to merge into your own, custom, 358*7c356e86SAndroid Build Coastguard Workerprompt (if you have one; otherwise pull this adaption to the default 359*7c356e86SAndroid Build Coastguard Workerone). Line 19 is the only other thing in this script rebinding the Ctrl-M 360*7c356e86SAndroid Build Coastguard Workerkey (which is normally produced by the Enter/Return key) to code that… 361*7c356e86SAndroid Build Coastguard Workerdoes <em>something crazy</em>. This trick however <em>does funny things with 362*7c356e86SAndroid Build Coastguard Workermultiline commands</em>, so if you type something out in multiple lines, 363*7c356e86SAndroid Build Coastguard Workerfor example <strong>here documents</strong> or <strong>loops</strong> press 364*7c356e86SAndroid Build Coastguard Worker<strong>Ctrl-J instead of Enter/Return</strong> after <em>each</em> line 365*7c356e86SAndroid Build Coastguard Workerincluding the first (at PS1) and final (at PS2) one. 366*7c356e86SAndroid Build Coastguard Worker---- 367*7c356e86SAndroid Build Coastguard WorkerToC: other-tty 368*7c356e86SAndroid Build Coastguard WorkerTitle: How do I start mksh on a specific terminal? 369*7c356e86SAndroid Build Coastguard Worker 370*7c356e86SAndroid Build Coastguard Worker<p>Normally: <tt>mksh -T<i>/dev/tty2</i></tt></p> 371*7c356e86SAndroid Build Coastguard Worker<p>However, if you want for it to return (e.g. for an embedded system rescue 372*7c356e86SAndroid Build Coastguard Worker shell), use this on your real console device instead: 373*7c356e86SAndroid Build Coastguard Worker <tt>mksh -T!<i>/dev/ttyACM0</i></tt></p> 374*7c356e86SAndroid Build Coastguard Worker<p>mksh can also daemonise (send to the background): 375*7c356e86SAndroid Build Coastguard Worker <tt>mksh -T- -c 'exec cdio lock'</tt></p> 376*7c356e86SAndroid Build Coastguard Worker---- 377*7c356e86SAndroid Build Coastguard WorkerToC: completion 378*7c356e86SAndroid Build Coastguard WorkerTitle: What about programmable tab completion? 379*7c356e86SAndroid Build Coastguard Worker 380*7c356e86SAndroid Build Coastguard WorkerThe shell itself provides static deterministic tab completion. 381*7c356e86SAndroid Build Coastguard WorkerHowever, you can use hooks like reprogramming the Tab key to a 382*7c356e86SAndroid Build Coastguard Workercommand line editor macro, and using the <tt>evaluate-region</tt> 383*7c356e86SAndroid Build Coastguard Workereditor command (modulo a bugfix) together with <tt>quote-region</tt> and shell functions to 384*7c356e86SAndroid Build Coastguard Workerimplement a programmable completion engine. Multiple people have 385*7c356e86SAndroid Build Coastguard Workerbeen considering doing so in our IRC channel; we’ll hyperlink to 386*7c356e86SAndroid Build Coastguard Workerthese engines when they are available. 387*7c356e86SAndroid Build Coastguard Worker---- 388*7c356e86SAndroid Build Coastguard WorkerToC: posix-mode 389*7c356e86SAndroid Build Coastguard WorkerTitle: How POSIX compliant is mksh? Also, UTF-8 vs. locales? 390*7c356e86SAndroid Build Coastguard Worker 391*7c356e86SAndroid Build Coastguard Worker<p>You’ll need to use the <tt>lksh</tt> binary, unless your C <tt>long</tt> 392*7c356e86SAndroid Build Coastguard Worker type is 32 bits wide, for POSIX-compliant arithmetic in the shell. This is 393*7c356e86SAndroid Build Coastguard Worker because <tt>mksh</tt> provides consistent, wraparound-defined, 32-bit 394*7c356e86SAndroid Build Coastguard Worker arithmetics on all platforms normally. You’ll also need to enable POSIX mode 395*7c356e86SAndroid Build Coastguard Worker (<tt>set -o posix</tt>) explicitly, which also disables brace expansion upon 396*7c356e86SAndroid Build Coastguard Worker being enabled (use <tt>set -o braceexpand</tt> to reenable if needed).</p> 397*7c356e86SAndroid Build Coastguard Worker<p>For the purpose of POSIX, mksh supports only the <tt>C</tt> locale. mksh’s 398*7c356e86SAndroid Build Coastguard Worker <tt>utf8-mode</tt> (which only supports the BMP (Basic Multilingual Plane) of 399*7c356e86SAndroid Build Coastguard Worker UCS and maps raw octets into the U+EF80‥U+EFFF wide character range; see 400*7c356e86SAndroid Build Coastguard Worker <tt>Arithmetic expressions</tt> in mksh(1) for details) <em>must</em> stay 401*7c356e86SAndroid Build Coastguard Worker disabled in POSIX mode (it is disabled upon enabling POSIX mode in R56+).</p> 402*7c356e86SAndroid Build Coastguard Worker<p class="boxhead">The following POSIX sh-compatible code toggles the 403*7c356e86SAndroid Build Coastguard Worker <tt>utf8-mode</tt> option dependent on the current POSIX locale, for mksh 404*7c356e86SAndroid Build Coastguard Worker to allow using the UTF-8 mode, within the constraints outlined above, in 405*7c356e86SAndroid Build Coastguard Worker code portable across various shell implementations:</p> 406*7c356e86SAndroid Build Coastguard Worker<div class="boxtext"> 407*7c356e86SAndroid Build Coastguard Worker <pre> 408*7c356e86SAndroid Build Coastguard Worker case ${KSH_VERSION:-} in 409*7c356e86SAndroid Build Coastguard Worker *MIRBSD KSH*|*LEGACY KSH*) 410*7c356e86SAndroid Build Coastguard Worker case ${LC_ALL:-${LC_CTYPE:-${LANG:-}}} in 411*7c356e86SAndroid Build Coastguard Worker *[Uu][Tt][Ff]8*|*[Uu][Tt][Ff]-8*) set -U ;; 412*7c356e86SAndroid Build Coastguard Worker *) set +U ;; 413*7c356e86SAndroid Build Coastguard Worker esac ;; 414*7c356e86SAndroid Build Coastguard Worker esac 415*7c356e86SAndroid Build Coastguard Worker </pre> 416*7c356e86SAndroid Build Coastguard Worker</div><p class="boxfoot">In near future, (UTF-8) locale tracking will 417*7c356e86SAndroid Build Coastguard Worker be implemented, though.</p> 418*7c356e86SAndroid Build Coastguard Worker<p>The shell is pretty close to POSIX, when run as <tt>lksh -o posix</tt> 419*7c356e86SAndroid Build Coastguard Worker under the "C" locale it is intended to match. It does not do everything 420*7c356e86SAndroid Build Coastguard Worker like other POSIX-compatible or ‑compliant shells, though.</p> 421*7c356e86SAndroid Build Coastguard Worker---- 422*7c356e86SAndroid Build Coastguard WorkerToC: function-local-scopes 423*7c356e86SAndroid Build Coastguard WorkerTitle: What differences in function-local scopes are there? 424*7c356e86SAndroid Build Coastguard Worker 425*7c356e86SAndroid Build Coastguard Worker<p><tt>mksh</tt> has a different scope model from AT&T <tt>ksh</tt>, 426*7c356e86SAndroid Build Coastguard Worker which leads to subtle differences in semantics for identical builtins. 427*7c356e86SAndroid Build Coastguard Worker This can cause issues with a <tt>nameref</tt> to suddenly point to a 428*7c356e86SAndroid Build Coastguard Worker local variable by accident. (Other common shells share mksh’s scoping 429*7c356e86SAndroid Build Coastguard Worker model.)</p> 430*7c356e86SAndroid Build Coastguard Worker<p class="boxhead">GNU <tt>bash</tt> allows unsetting local variables; in 431*7c356e86SAndroid Build Coastguard Worker <tt>mksh</tt>, doing so in a function allows back access to the global 432*7c356e86SAndroid Build Coastguard Worker variable (actually the one in the next scope up) with the same name. The 433*7c356e86SAndroid Build Coastguard Worker following code, when run before function definitions, changes the behaviour 434*7c356e86SAndroid Build Coastguard Worker of <tt>unset</tt> to behave like other shells (the alias can be removed 435*7c356e86SAndroid Build Coastguard Worker after the definitions):</p> 436*7c356e86SAndroid Build Coastguard Worker<div class="boxtext"> 437*7c356e86SAndroid Build Coastguard Worker <pre> 438*7c356e86SAndroid Build Coastguard Worker case ${KSH_VERSION:-} in 439*7c356e86SAndroid Build Coastguard Worker *MIRBSD KSH*|*LEGACY KSH*) 440*7c356e86SAndroid Build Coastguard Worker function unset_compat { 441*7c356e86SAndroid Build Coastguard Worker \\builtin typeset unset_compat_x 442*7c356e86SAndroid Build Coastguard Worker 443*7c356e86SAndroid Build Coastguard Worker for unset_compat_x in "$@"; do 444*7c356e86SAndroid Build Coastguard Worker eval "\\\\builtin unset $unset_compat_x[*]" 445*7c356e86SAndroid Build Coastguard Worker done 446*7c356e86SAndroid Build Coastguard Worker } 447*7c356e86SAndroid Build Coastguard Worker \\builtin alias unset=unset_compat 448*7c356e86SAndroid Build Coastguard Worker ;; 449*7c356e86SAndroid Build Coastguard Worker esac 450*7c356e86SAndroid Build Coastguard Worker </pre> 451*7c356e86SAndroid Build Coastguard Worker</div><p class="boxfoot">When a local variable is created (e.g. using 452*7c356e86SAndroid Build Coastguard Worker <tt>local</tt>, <tt>typeset</tt>, <tt>integer</tt> or 453*7c356e86SAndroid Build Coastguard Worker <tt>\\builtin typeset</tt>) it does not, like in other shells, inherit 454*7c356e86SAndroid Build Coastguard Worker the value from the global (next scope up) variable with the same name; 455*7c356e86SAndroid Build Coastguard Worker it is rather created without any value (unset but defined).</p> 456*7c356e86SAndroid Build Coastguard Worker---- 457*7c356e86SAndroid Build Coastguard WorkerToC: regex-comparison 458*7c356e86SAndroid Build Coastguard WorkerTitle: I get an error in this regex comparison 459*7c356e86SAndroid Build Coastguard Worker 460*7c356e86SAndroid Build Coastguard Worker<p>Use extglobs instead of regexes:<br /> 461*7c356e86SAndroid Build Coastguard Worker <tt>[[ foo =~ (foo|bar).*baz ]]</tt><br /> 462*7c356e86SAndroid Build Coastguard Worker … becomes…<br /> 463*7c356e86SAndroid Build Coastguard Worker <tt>[[ foo = *@(foo|bar)*baz* ]]</tt></p> 464*7c356e86SAndroid Build Coastguard Worker---- 465*7c356e86SAndroid Build Coastguard WorkerToC: trim-vector 466*7c356e86SAndroid Build Coastguard WorkerTitle: ${@?}: bad substitution 467*7c356e86SAndroid Build Coastguard Worker 468*7c356e86SAndroid Build Coastguard Worker<p>In mksh, you cannot assign to or trim a vector (yet). For most 469*7c356e86SAndroid Build Coastguard Worker cases it is possible to write the affected code in a way avoiding 470*7c356e86SAndroid Build Coastguard Worker this extension; for example, trimming <tt>${@#foo}</tt> could be 471*7c356e86SAndroid Build Coastguard Worker applied to <tt>$1</tt> only and <tt>${@?}</tt> can be replaced 472*7c356e86SAndroid Build Coastguard Worker with a test whether <tt>$# -eq 0</tt>.</p> 473*7c356e86SAndroid Build Coastguard Worker---- 474*7c356e86SAndroid Build Coastguard WorkerToC: extensions-to-avoid 475*7c356e86SAndroid Build Coastguard WorkerTitle: Are there any extensions to avoid? 476*7c356e86SAndroid Build Coastguard Worker 477*7c356e86SAndroid Build Coastguard Worker<p>GNU <tt>bash</tt> supports “<tt>&></tt>” (and “|&”) to redirect 478*7c356e86SAndroid Build Coastguard Worker both stdout and stderr in one go, but this breaks POSIX and Korn Shell syntax; 479*7c356e86SAndroid Build Coastguard Worker use POSIX redirections instead:</p> 480*7c356e86SAndroid Build Coastguard Worker<table border="1" cellpadding="3"> 481*7c356e86SAndroid Build Coastguard Worker <tr><td>GNU bash</td><td> 482*7c356e86SAndroid Build Coastguard Worker <tt>foo |& bar |& baz &>log</tt> 483*7c356e86SAndroid Build Coastguard Worker </td></tr> 484*7c356e86SAndroid Build Coastguard Worker <tr><td>POSIX</td><td> 485*7c356e86SAndroid Build Coastguard Worker <tt>foo 2>&1 | bar 2>&1 | baz >log 2>&1</tt> 486*7c356e86SAndroid Build Coastguard Worker </td></tr> 487*7c356e86SAndroid Build Coastguard Worker</table> 488*7c356e86SAndroid Build Coastguard Worker---- 489*7c356e86SAndroid Build Coastguard WorkerToC: while-read-pipe 490*7c356e86SAndroid Build Coastguard WorkerTitle: Something is going wrong with my while...read loop 491*7c356e86SAndroid Build Coastguard Worker 492*7c356e86SAndroid Build Coastguard Worker<p class="boxhead">Most likely, you’ve encountered the problem in which 493*7c356e86SAndroid Build Coastguard Worker the shell runs all parts of a pipeline as subshell. The inner loop will 494*7c356e86SAndroid Build Coastguard Worker be executed in a subshell and variable changes cannot be propagated if 495*7c356e86SAndroid Build Coastguard Worker run in a pipeline:</p> 496*7c356e86SAndroid Build Coastguard Worker<div class="boxtext"> 497*7c356e86SAndroid Build Coastguard Worker <pre> 498*7c356e86SAndroid Build Coastguard Worker bar | baz | while read foo; do ...; done 499*7c356e86SAndroid Build Coastguard Worker </pre> 500*7c356e86SAndroid Build Coastguard Worker</div><p class="boxfoot">Note that <tt>exit</tt> in the inner loop will 501*7c356e86SAndroid Build Coastguard Worker also only exit the subshell and not the original shell. Likewise, if the 502*7c356e86SAndroid Build Coastguard Worker code is inside a function, <tt>return</tt> in the inner loop will only 503*7c356e86SAndroid Build Coastguard Worker exit the subshell and won’t terminate the function.</p> 504*7c356e86SAndroid Build Coastguard Worker<p class="boxhead">Use co-processes instead:</p> 505*7c356e86SAndroid Build Coastguard Worker<div class="boxtext"> 506*7c356e86SAndroid Build Coastguard Worker <pre> 507*7c356e86SAndroid Build Coastguard Worker bar | baz |& 508*7c356e86SAndroid Build Coastguard Worker while read -p foo; do ...; done 509*7c356e86SAndroid Build Coastguard Worker exec 3>&p; exec 3>&- 510*7c356e86SAndroid Build Coastguard Worker </pre> 511*7c356e86SAndroid Build Coastguard Worker</div><p class="boxfoot">If <tt>read</tt> is run in a way such as 512*7c356e86SAndroid Build Coastguard Worker <tt>while read foo; do ...; done</tt> then leading whitespace will be 513*7c356e86SAndroid Build Coastguard Worker removed (IFS) and backslashes processed. You might want to use 514*7c356e86SAndroid Build Coastguard Worker <tt>while IFS= read -r foo; do ...; done</tt> for pristine I/O.</p> 515*7c356e86SAndroid Build Coastguard Worker<p class="boxhead">Similarly, when using the <tt>-a</tt> option, use of the 516*7c356e86SAndroid Build Coastguard Worker <tt>-r</tt> option might be prudent (<tt>read -raN-1 arr <file</tt>); 517*7c356e86SAndroid Build Coastguard Worker the same applies for NUL-terminated lines:</p> 518*7c356e86SAndroid Build Coastguard Worker<div class="boxtext"> 519*7c356e86SAndroid Build Coastguard Worker <pre> 520*7c356e86SAndroid Build Coastguard Worker find . -type f -print0 |& \ 521*7c356e86SAndroid Build Coastguard Worker while IFS= read -d '' -pr filename; do 522*7c356e86SAndroid Build Coastguard Worker print -r -- "found <${filename#./}>" 523*7c356e86SAndroid Build Coastguard Worker done 524*7c356e86SAndroid Build Coastguard Worker </pre> 525*7c356e86SAndroid Build Coastguard Worker</div> 526*7c356e86SAndroid Build Coastguard Worker---- 527*7c356e86SAndroid Build Coastguard WorkerToC: command-alias 528*7c356e86SAndroid Build Coastguard WorkerTitle: “command” doesn’t expand aliases as in ksh93 529*7c356e86SAndroid Build Coastguard Worker 530*7c356e86SAndroid Build Coastguard WorkerThis is because AT&T ksh93 ships a predefined alias enabling this:<br /> 531*7c356e86SAndroid Build Coastguard Worker<tt>alias command='command '</tt><br /> 532*7c356e86SAndroid Build Coastguard Workerput this into your <tt>~/.mkshrc</tt> 533*7c356e86SAndroid Build Coastguard Worker(note the space before the closing single quote) 534*7c356e86SAndroid Build Coastguard Worker---- 535*7c356e86SAndroid Build Coastguard WorkerToC: builtin-rename 536*7c356e86SAndroid Build Coastguard WorkerTitle: “rename” doesn’t work as expected! 537*7c356e86SAndroid Build Coastguard Worker 538*7c356e86SAndroid Build Coastguard Worker<p>There’s a <tt>rename</tt> built-in utility in mksh, which is a very 539*7c356e86SAndroid Build Coastguard Worker thin wrapper around the rename(2) syscall. It receives two pathnames, 540*7c356e86SAndroid Build Coastguard Worker source and destination where the first is then atomically renamed to 541*7c356e86SAndroid Build Coastguard Worker the latter. It does not move, i.e. fails for different filesystems.</p> 542*7c356e86SAndroid Build Coastguard Worker<p>The GNU package <tt>util-linux</tt> has a different <tt>rename</tt> 543*7c356e86SAndroid Build Coastguard Worker command. If you wish to invoke an external utility (in favour over a 544*7c356e86SAndroid Build Coastguard Worker builtin), you can use <tt>dot.mkshrc</tt>’s function <tt>enable</tt> 545*7c356e86SAndroid Build Coastguard Worker or put the following into your <tt>~/.mkshrc</tt>:</p> 546*7c356e86SAndroid Build Coastguard Worker<pre>alias rename="$(whence -p rename)"</pre> 547*7c356e86SAndroid Build Coastguard Worker---- 548*7c356e86SAndroid Build Coastguard WorkerToC: builtin-sleep 549*7c356e86SAndroid Build Coastguard WorkerTitle: “sleep” does not accept ‘m’ for minutes! 550*7c356e86SAndroid Build Coastguard Worker 551*7c356e86SAndroid Build Coastguard Worker<p>mksh contains a <tt>sleep</tt> built-in utility, in order to be 552*7c356e86SAndroid Build Coastguard Worker able to offer sub-second sleep to shell scripts for most platforms. 553*7c356e86SAndroid Build Coastguard Worker (It does not exist if the platform lacks select(2) — which should 554*7c356e86SAndroid Build Coastguard Worker be rare.)</p> 555*7c356e86SAndroid Build Coastguard Worker<p>GNU coreutils contains a sleep implementation accepting suffixed 556*7c356e86SAndroid Build Coastguard Worker numbers. If you wish to invoke an external utility (in favour over a 557*7c356e86SAndroid Build Coastguard Worker builtin), you can use <tt>dot.mkshrc</tt>’s function <tt>enable</tt> 558*7c356e86SAndroid Build Coastguard Worker or put something along the following lines into <tt>~/.mkshrc</tt>:</p> 559*7c356e86SAndroid Build Coastguard Worker<pre>alias sleep="$(whence -p sleep)"</pre> 560*7c356e86SAndroid Build Coastguard Worker<pre>timer() { sleep $(($1*60${2:++$2})); } # timer mins [secs]</pre> 561*7c356e86SAndroid Build Coastguard Worker<pre>timer() { 562*7c356e86SAndroid Build Coastguard Worker local arg=${1/m/'*60+'} 563*7c356e86SAndroid Build Coastguard Worker [[ $arg = *+ ]] && arg+=0 564*7c356e86SAndroid Build Coastguard Worker sleep $(($arg) 565*7c356e86SAndroid Build Coastguard Worker}</pre> 566*7c356e86SAndroid Build Coastguard Worker---- 567*7c356e86SAndroid Build Coastguard WorkerToC: string-concat 568*7c356e86SAndroid Build Coastguard WorkerTitle: “+=” behaves differently from other shells 569*7c356e86SAndroid Build Coastguard Worker 570*7c356e86SAndroid Build Coastguard Worker<p>In POSIX shell, “=” in code like <tt>var=content</tt> is a string 571*7c356e86SAndroid Build Coastguard Worker assignment, always. You can use <tt>var=$((content))</tt> for an 572*7c356e86SAndroid Build Coastguard Worker arithmetic assignment that mostly uses C language rules.</p> 573*7c356e86SAndroid Build Coastguard Worker<p>It stands to consider that the common shell extension “+=” as in 574*7c356e86SAndroid Build Coastguard Worker <tt>var+=content</tt> would always do string concatenation; it does 575*7c356e86SAndroid Build Coastguard Worker in mksh, but not in some other shells, in which, when <tt>var</tt> has 576*7c356e86SAndroid Build Coastguard Worker been declared integer, addition is done instead.</p> 577*7c356e86SAndroid Build Coastguard Worker<p>You can make the code portable by using “((…))” (a.k.a. <tt>let</tt>) 578*7c356e86SAndroid Build Coastguard Worker instead: <tt>(( var += content ))</tt> does arithmetic addition in 579*7c356e86SAndroid Build Coastguard Worker all shells involved.</p> 580*7c356e86SAndroid Build Coastguard Worker---- 581*7c356e86SAndroid Build Coastguard WorkerToC: set-e 582*7c356e86SAndroid Build Coastguard WorkerTitle: I use “set -e” and my code unexpectedly errors out 583*7c356e86SAndroid Build Coastguard Worker 584*7c356e86SAndroid Build Coastguard Worker<p>I personally recommend people to not use “<tt>set -e</tt>”, as it 585*7c356e86SAndroid Build Coastguard Workermakes error handling more difficult. However, some insist. There have 586*7c356e86SAndroid Build Coastguard Workerbeen bugfixes (relative to e.g. oksh/loksh and posh) in this aspect, 587*7c356e86SAndroid Build Coastguard Workerand the user has to make sure <tt>$?</tt> is always 0 ASAP even after 588*7c356e86SAndroid Build Coastguard Workera command that doesn’t check it.</p> 589*7c356e86SAndroid Build Coastguard Worker<pre>istwo() { 590*7c356e86SAndroid Build Coastguard Worker for i in "$@"; do 591*7c356e86SAndroid Build Coastguard Worker test x"$i" = x"2" && echo two 592*7c356e86SAndroid Build Coastguard Worker done 593*7c356e86SAndroid Build Coastguard Worker} 594*7c356e86SAndroid Build Coastguard Workerset -e 595*7c356e86SAndroid Build Coastguard Workeristwo 1 596*7c356e86SAndroid Build Coastguard Workerecho END</pre> 597*7c356e86SAndroid Build Coastguard Worker<p>This can be fixed by either adding an explicit “<tt>:</tt>” (or 598*7c356e86SAndroid Build Coastguard Worker“<tt>true</tt>”) after the comparison, or even…</p> 599*7c356e86SAndroid Build Coastguard Worker<pre>test x"$i" = x"2" && echo two || :</pre> 600*7c356e86SAndroid Build Coastguard Worker<p>… or right after the <tt>done</tt> inside the function, but…</p> 601*7c356e86SAndroid Build Coastguard Worker<pre>test x"$i" != x"2" || echo two</pre> 602*7c356e86SAndroid Build Coastguard Worker<p>… negating the condition and using “<tt>||</tt>” is preferable.</p> 603*7c356e86SAndroid Build Coastguard Worker 604*7c356e86SAndroid Build Coastguard Worker<p>Remember that Korn shell-style functions (with <tt>function</tt> 605*7c356e86SAndroid Build Coastguard Worker keyword and <strong>without</strong> parenthesēs) in AT&T ksh93 606*7c356e86SAndroid Build Coastguard Worker and mksh R51 and up have their own shell option scope, but while…</p> 607*7c356e86SAndroid Build Coastguard Worker<pre>function istwo { 608*7c356e86SAndroid Build Coastguard Worker set +e 609*7c356e86SAndroid Build Coastguard Worker … 610*7c356e86SAndroid Build Coastguard Worker}</pre> 611*7c356e86SAndroid Build Coastguard Worker<p>… might help in error handling, the return status of a function is 612*7c356e86SAndroid Build Coastguard Worker still the last errorlevel inside, so an explicit true (“<tt>:</tt>”) 613*7c356e86SAndroid Build Coastguard Worker or, more explicitly, “<tt>return 0</tt>” at its end is still needed 614*7c356e86SAndroid Build Coastguard Worker if the <em>caller</em> runs under <tt>set -e</tt>.</p> 615*7c356e86SAndroid Build Coastguard Worker---- 616*7c356e86SAndroid Build Coastguard WorkerToC: set-eo-pipefail 617*7c356e86SAndroid Build Coastguard WorkerTitle: I use “set -eo pipefail” and my code unexpectedly errors out 618*7c356e86SAndroid Build Coastguard Worker 619*7c356e86SAndroid Build Coastguard Worker<p class="boxhead">Related to the above FAQ entry, using 620*7c356e86SAndroid Build Coastguard Worker <tt>set -o pipefail</tt> makes the following construct error out:</p> 621*7c356e86SAndroid Build Coastguard Worker<div class="boxtext"> 622*7c356e86SAndroid Build Coastguard Worker <pre> 623*7c356e86SAndroid Build Coastguard Worker set -e 624*7c356e86SAndroid Build Coastguard Worker for x in 1 2; do 625*7c356e86SAndroid Build Coastguard Worker false && echo $x 626*7c356e86SAndroid Build Coastguard Worker done | cat 627*7c356e86SAndroid Build Coastguard Worker </pre> 628*7c356e86SAndroid Build Coastguard Worker</div><p class="boxfoot">This is because, while the <tt>&&</tt> 629*7c356e86SAndroid Build Coastguard Worker ensures that the inner command’s failure is not taken, it sets the entire 630*7c356e86SAndroid Build Coastguard Worker <tt>for</tt>‥<tt>done</tt> loop’s errorlevel, which is passed on by 631*7c356e86SAndroid Build Coastguard Worker <tt>-o pipefail</tt>.</p> 632*7c356e86SAndroid Build Coastguard Worker<p>Invert the inner command:<br /> 633*7c356e86SAndroid Build Coastguard Worker <tt>true || echo $x</tt></p> 634*7c356e86SAndroid Build Coastguard Worker---- 635*7c356e86SAndroid Build Coastguard WorkerToC: faq 636*7c356e86SAndroid Build Coastguard WorkerTitle: My question is not answered here! 637*7c356e86SAndroid Build Coastguard Worker 638*7c356e86SAndroid Build Coastguard WorkerDo read the mksh(1) manual page. You might also wish to read the <a 639*7c356e86SAndroid Build Coastguard Worker href="@@RELPATH@@ksh-chan.htm">homepage of the <tt>#ksh</tt> IRC channel 640*7c356e86SAndroid Build Coastguard Workeron Freenode</a> which lists several resources for Korn or POSIX-compatible 641*7c356e86SAndroid Build Coastguard Workershells in general. Or, <a href="#contact">contact</a> us (developer and 642*7c356e86SAndroid Build Coastguard Workerusers), for example via IRC. 643*7c356e86SAndroid Build Coastguard Worker---- 644*7c356e86SAndroid Build Coastguard WorkerToC: contact 645*7c356e86SAndroid Build Coastguard WorkerTitle: How do I contact you (to say thanks)? 646*7c356e86SAndroid Build Coastguard Worker 647*7c356e86SAndroid Build Coastguard WorkerYou can say hi in the <tt>#!/bin/mksh</tt> channel on Freenode <a 648*7c356e86SAndroid Build Coastguard Worker href="@@RELPATH@@irc.htm">IRC</a>, although a <a 649*7c356e86SAndroid Build Coastguard Worker href="@@RELPATH@@danke.htm">donation</a> wouldn’t be amiss ☺ The <a 650*7c356e86SAndroid Build Coastguard Worker href="http://www.mail-archive.com/[email protected]/">mailing 651*7c356e86SAndroid Build Coastguard Workerlist</a> can also be used. 652*7c356e86SAndroid Build Coastguard Worker---- 653