Using the Korn Shell with Linux
Posted by: Xjlittle
My current consulting gig requires that I use the Korn Shell and modify Unix scripts so that they will work with Linux. While the Korn Shell has many comparable characteristics of BASH there are some distinct differences-or at least ones that I’ve never seen in BASH.
The first difference that I noticed is tab completion. For example let’s say that I issue the command
ls /home/jlittle
and hit the tab key to see the files and directories. The output that you see will be in this format
ls /home/jlittle/
1) CentOS-5.2-x86_64-bin-DVD/
2) Desktop/
3) Documents/
4) Video call snapshot 8.png
5) bin/
6) ffmpeg.cfg
At this point you can either choose a number and hit the tab key or type in the first couple of letter of what you want to see or do. The complete output when using the number would look like this
ls /home/jlittle/<tab>
1) CentOS-5.2-x86_64-bin-DVD/
2) Desktop/
3) Documents/
4) Video call snapshot 8.png
5) bin/
6) ffmpeg.cfg
ls /home/jlittle/Desktop/<2tab>
Project-timeSheet.ods Skype.desktop
Typing 2 tab and the tab completion gives us the listing of the /home/jlittle/. Kind of a cool way of doing tab completion don’t you think?
You should also not use the “test” built-in that is available in bash. In bash the test built-in is the same as the “[" built-in. In other words don't use
if test $# -gt 0; then
instead use:
if [ $# -gt 0 ]; then
The korn shell also prefers the use of double brackets syntax "[[ ]]" instead of single brackets. This adds additional operators such as && and ||:
if [[ $# -gt 0 && $? -eq 0 ]]; then
You can use && and || to construct shorthand for an "if" statement in the case where the if statement has a single consequent line:
[ $# -eq 0 ] && exit 0
The Korn Shell is a powerful tool that can make your job easier. Since it's creation several features have been added while maintaining backwards compatibility with the Bourne shell. The Korn shell can also be used as a programming language which gives it a distinct advantage of typical Unix and Linux shells.
Give ksh a whirl. I haven't even scratched the surface of what the Korn shell can do for your scripting. If you are used to scripting with Bash then learning the Korn shell should only have a mild learning curve while presenting you with additional scripting power and speed.
-j




