bsddog | VIDEO | ARTICLE | BLOG | TAG | PROJECT | CONTACT | DONATE
bsddog

Korn Shell (ksh) Tips and Tricks

Korn Shell (ksh) Scripting and Programming Essential Guide for OpenBSD, Linux, Windows, etc.

With few exceptions, I do not take notes, and won't encourage anyone to do so either. However, sometimes, a list of some tips and trick may help neophytes. Korn Shell, also known as ksh has different flavours and versions: pdksh, mksh, dtksh, ksh86, ksh88, ksh93, ksh93u+, ksh93v- , ksh93u+m , ksh2020, etc. There's even a portable port of OpenBSD battery-included version of Korn Shell on the GitHub.

Prologue: Korn Shell in the Realm of Shell and Scripting

Korn Shell is the industry-standard of Command Line Interface (CLI) aka 'shell' for a UNIX or UNIX-ish Operating System (OS). Korn Shell is both 'shell', and also a 'scripting language' which are two separate things, by the way. But as it happened, and AFAIK, almost every single of such programs are both 'shell' and 'scripting language'. Discussion about the difference between the two is beyond of this article. Maybe later.

When it comes down to scripting, unfortunately a man has to stick to POSIX, i.e. using a sub-set of Korn Shell. That's a loosely description of whole shebang, but for now that's nice enough to be able to continue. One thing more: portability is over-rated.

I stick to standards the only UNIX circle, which has kept the UNIX way of doing things, i.e. OpenBSD. If you ask Normies and Normie-writers — which are working for Normie-friendly publishers, e.g. Packt, No Starch, etc. — what's special about OpenBSD, they will go on a typical rant of security, correctness, blah blah blah. That's a cliché. I hate cliché. OpenBSD is special because:

It's the only UNIX-ish OS, out there, in which has the spirit of AT&T Bell Labs.

I'm well aware of the fact that OpenBSD's derived from 4.4BSD-lite2, BSD/net2, … but that's not the point. Innovation is the point which was the prime characteristic of AT&T Bell Labs in general, and more specifically Ken Thompson. Although it's crucial, but even licence doesn't matter: ISC, BSD whatever… — What about GCC 4.2.1 situation then? OK! Licence matters. — Without innovation, you're just another 'master-of-baiting monkeys', like Linux and FreeBSD.

I'm going to update this page frequently, more lines will come, and I don't give a damn about SEO and Normie-friendly aspects of the page. I'll manage the page in way, so that, when I visit it, to summon up some memory, it would gives me a glorious joy. Do not be a nerd.

Which flavours of Shells Are Acceptable on OpenBSD, Linux and Windows?

Regular men, like I/me (That's mu pronouns. Just kidding!), are using something from the following list. The same rule applies to all systems, including: OpenBSD, Linux (man! GNU/Linux man!), Windows (WSL, WSL2, MinGW, Cygwin, MSYS2) and Atari 2600. As a rule of thumb, FreeBSD-circle are absolutely wrong about absolute everything. So I don't care what they think.

  • $ echo $KSH_VERSION
  • OpenBSD : @(#)PD KSH v5.2.14
  • MSYS2…: @(#)MIRBSD KSH R59
  • WSL 1/2 : @(#)MIRBSD KSH R59

P.S. On Problem of FreeBSD Circle Ethics

Just one quick off-topic note on something that I believe is more important than any tools, specially computer and PCs:

A good chunk of men and women who are — directly or indirectly — associated with FreeBSD Foundation, Core and Project, and also some (who knows how many …) of their users, have been actively promoting and supporting different types of perversions, social and marital degeneracy, with the goal of destruction of your family and society. They're doing that openly in public, using different social platforms, mainly Twitter and their Mastodon instances: promoting the ANTI-orthodoxy of living your life. That's BAD, very BAD. A word of advice for parents:

Keep your children away from FreeBSD and their associates, if you value your family and traditional orthodoxy. It's up to you, anyway; but I had a moral responsibility to inform you about dangers around such projects; you've been warned.

Shell Special Characters, Meta Characters, Reserved Words, etc.

Shell Whitespace Characters

Shell has three (3) shell whitespace characters:

  • space
  • tab
  • newline

Shell Meta Characters

Shell has seven (7) meta-characters:

  • <
  • >
  • (
  • )
  • |
  • &
  • ;

Shell Redirection Tokens

Shell has eleven (11) redirection tokens, in which are being built using Shell seven (7) meta-characters:

  • <
  • >
  • <<
  • >>
  • <&
  • >&
  • <<-
  • <>
  • >|
  • <&p
  • >&p

Shell Special Characters

Shell has twelve (12) special characters:

  • \
  • "
  • '
  • #
  • $
  • `
  • ~
  • {
  • }
  • *
  • ?
  • [

Shell Very Special Characters

Shell has three (3) very special characters:

  • =
  • %
  • @

Shell Reserved Words (in a [:punct:] Form)

Shell has nine (9) reserved words (in a [:punct:] form):

  • !
  • (
  • )
  • ((
  • ))
  • {
  • }
  • [[
  • ]]

Shell Reserved Words (in an [:alnum:] Form)

Shell has seventeen (17) reserved words (in an [:alnum:] form):

  • case
  • esac
  • if
  • else
  • then
  • elif
  • fi
  • while
  • until
  • for
  • do
  • done
  • in
  • name
  • select
  • function
  • time

Shell Special Built-in Commands

Shell has fifteen (15) special built-in commands:

  • .
  • :
  • set
  • unset
  • export
  • eval
  • exec
  • exit
  • return
  • break
  • continue
  • readonly
  • shift
  • times
  • trap

Shell Built-in Commands

Shell has sixteen (16) built-in commands:

  • alias
  • bg
  • cd
  • command
  • false
  • fc
  • fg
  • getopts
  • jobs
  • kill
  • pwd
  • read
  • true
  • umask
  • unalias
  • wait

Korn Shell Built-in Commands

Korn Shell has eight (8) built-in commands:

  • [
  • echo
  • let
  • print
  • suspend
  • test
  • ulimit
  • whence

Korn Shell Special Built-in Commands

Korn Shell has two (2) special built-in commands:

  • builtin
  • typeset

Korn Very Special Built-in Commands (POSIX-Off)

Korn Shell has four (4) very special built-in commands, i.e. when POSIX mode is off:

  • alias
  • readonly
  • set
  • typeset

Korn Shell I/O Redirectors

Korn Shell has fifteen (15) I/O redirectors:

  • n<f
  • n>f
  • <<l
  • n>>f
  • n<&n
  • n>&n
  • <<-l
  • n<>f
  • n>|f
  • n<&p
  • n>&p
  • n<&-
  • n>&-
  • c|c
  • |&

Notice:

  • 'n' stands for file-descriptor number.
  • 'f' stands for filename.
  • 'l' stands for label.
  • 'c' stands for command.
  • 'p' is a literal 'p', i.e. character constant 'p'.

Shell List Tokens

Shell has five (5) list tokens:

  • &&
  • ||
  • &
  • |&
  • ;

Shell Terminology

Shell Substitution and Expansion Terms

There are fourteen (14) distinct terms, related to substitution and expansion, in the Korn Shell:

  • Positional Parameter
  • Pipiline Reading
  • Word Splitting
  • Alias Substitution
  • Tilde Expansion
  • Parameter Expansion
  • Command Substitution
  • Arithmetic Expansion
  • Field Splitting
  • Brace Expansion (Alternation)
  • Pathname Expansion (Globbing)
  • Pathname Expression Expansion
  • Pattern Matching
  • Parameter Assignment

― by BSDDOG

#Blog | #OpenBSD