Questions tagged [ksh]

The KornShell is an open source, POSIX-compatible shell language from AT&T based upon the original Bourne shell. Make sure you know whether your ksh is ksh93 or a clone.

The KornShell was created by David Korn at AT&T. It was created at Bell Labs (then AT&T) as a logical successor to the Bourne shell as the UNIX command line interpreter and scripting language.

The KornShell is compatible in syntax with the original Bourne shell and adds several features, like an interactive command line editor, functions with local variables, and arithmetic.

History

The KornShell was originally distributed as part of the AT&T Toolchest in the early 80's.

There are many different versions of the KornShell, and it can make a difference to know what they are.

Ksh93 is currently at version [ksh93u+] (For a long time it was distributed by AT&T, now it has moved to GitHub https://github.com/att/ast) and distributed under the Eclipse Public License as part of the AST (AT&T Software Tools) project staffed by David Korn and Glenn Fowler.

  • Ksh93 precursor ksh88 was the last official version of the KornShell in the AT&T Toolchest.
  • Mortice Kern Systems ported ksh88 to DOS/Windows, and adopted by Microsoft into its UNIX toolsuite for windows. Microsoft eventually shifted to Interix for its UNIX toolsuite.
  • UNIX vendors incorporated ksh88 into their product and enhanced it (Sun Microsystems added internationalisation).
  • pdksh was distributed for UNIX (like) systems, but missed some features of the original.
  • mksh is the contemporary successor of pdksh; David Korn agreed it’s a good thing as long as it cannot be confused with the original ksh, so please add the tag for questions related to it (it’s on-topic in most ksh-related places)
  • ksh93 has been under development by the AST team since its inception, with roughly 1 major update per year. For a long time ksh93 was only available commercially, but eventually was open-sourced.

Features

The following list includes features of ksh88 and ksh93

  • command line editing - vi or emacs mode
  • command line history - use vi/emacs/arrow keys to recall earlier commands

  • Functions

  • local variables in functions
  • arithmetic (( ... )) and $(( ... ))
  • floating point arithmetic (since ksh93)
  • builtin replacements for many standard unix commands

Standardization

The KornShell (ksh93) was at the basis of the POSIX sh standard, although not all ksh93 features are included, and some posix shell features are optional.

2730 questions
2823
votes
17 answers

How to mkdir only if a directory does not already exist?

I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the mkdir command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that…
Spike Williams
  • 35,795
  • 13
  • 48
  • 60
334
votes
23 answers

How to detect if a script is being sourced

I have a script where I do not want it to call exit if it's being sourced. I thought of checking if $0 == bash but this has problems if the script is sourced from another script, or if the user sources it from a different shell like ksh. Is…
brianegge
  • 29,240
  • 13
  • 74
  • 99
321
votes
6 answers

What is the difference between $(command) and `command` in shell programming?

To store the output of a command as a variable in sh/ksh/bash, you can do either var=$(command) or var=`command` What's the difference if any between the two methods?
hhafez
  • 38,949
  • 39
  • 113
  • 143
315
votes
8 answers

Select unique or distinct values from a list in UNIX shell script

I have a ksh script that returns a long list of values, newline separated, and I want to see only the unique/distinct values. It is possible to do this? For example, say my output is file suffixes in a…
brabster
  • 42,504
  • 27
  • 146
  • 186
235
votes
8 answers

How to get the second column from command output?

My command's output is something like: 1540 "A B" 6 "C" 119 "D" The first column is always a number, followed by a space, then a double-quoted string. My purpose is to get the second column only, like: "A B" "C" "D" I intended to use…
Qiang Xu
  • 4,353
  • 8
  • 36
  • 45
160
votes
2 answers

What does it mean in shell when we put a command inside dollar sign and parentheses: $(command)

I just want to understand following line of code in shell. It is used to get the current working directory. I am aware that $(variable) name return the value inside the variable name, but what is $(command) supposed to return? Does it return the…
KItis
  • 5,476
  • 19
  • 64
  • 112
101
votes
5 answers

How can I find a file/directory that could be anywhere on linux command line?

Ideally, I would be able to use a program like find [file or directory name] to report the paths with matching filenames/directories. Unfortunately this seems to only check the current directory, not the entire folder. I've also tried locate and…
johncorser
  • 9,262
  • 17
  • 57
  • 102
86
votes
2 answers

Shell Script: How to write a string to file and to stdout on console?

How to write a string to file and to stdout on console? If I do echo "hello" > logfile.txt I view only hello in logfile.txt but how can I write hello also on console of Linux?
Catanzaro
  • 1,312
  • 2
  • 13
  • 24
74
votes
8 answers

Single command to create a file and set its permission

I am using the following 2 commands to create a 0B file and set its extn to 644 touch filename.ext chmod 777 filename.txt My question is that whether there is any single command in unix (korn shell) that will do the two things together that is to…
user3075776
  • 741
  • 1
  • 5
  • 4
73
votes
4 answers

How to get pid given the process name

Hi I have searched various forums and here as well, I could find some answers for Linux and Mac but not able to find solution for Unix and specially Korn Shell. How to get process name (command name) from process id (pid) Below reference I found…
gahlot.jaggs
  • 1,083
  • 3
  • 11
  • 21
71
votes
12 answers

Bash or KornShell (ksh)?

I am not new to *nix, however lately I have been spending a lot of time at the prompt. My question is what are the advantages of using KornShell (ksh) or Bash Shell? Where are the pitfalls of using one over the other? Looking to understand from the…
user13158
  • 721
  • 1
  • 6
  • 4
60
votes
3 answers

Pipe input into a script

I have written a shell script in ksh to convert a CSV file into Spreadsheet XML file. It takes an existing CSV file (the path to which is a variable in the script), and then creates a new output file .xls. The script has no positional parameters.…
Ben Hamilton
  • 949
  • 3
  • 10
  • 21
59
votes
4 answers

Grab the filename in Unix out of full path

I am trying to get "abc.txt" out of /this/is/could/be/any/path/abc.txt using Unix command. Note that /this/is/could/be/any/path is dynamic. Any idea?
iwan
  • 7,269
  • 18
  • 48
  • 66
56
votes
5 answers

Get the exit code for a command in Bash and KornShell (ksh)

I want to write code like this: command="some command" safeRunCommand $command safeRunCommand() { cmnd=$1 $($cmnd) if [ $? != 0 ]; then printf "Error when executing command: '$command'" exit $ERROR_CODE fi } But this…
Kolesar
  • 1,265
  • 3
  • 19
  • 41
51
votes
6 answers

Have Find print just the filenames, not full paths

I'm using the find command in a ksh script, and I'm trying to retrieve just the filenames, rather than the full path. As in, I want it to return text.exe, not //severname/dir1/dir2/text.exe. How would I go about getting that? To clarify, I know the…
Steve
  • 4,457
  • 12
  • 48
  • 89
1
2 3
99 100