Differences between version 3 and previous revision of ShellPortabilityNotes.
Other diffs: Previous Major Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 3 | Last edited on Thursday, May 12, 2005 1:03:07 pm | by JohnMcPherson | Revert |
Older page: | version 2 | Last edited on Tuesday, January 25, 2005 12:29:30 pm | by JohnMcPherson | Revert |
@@ -1,60 +1,89 @@
Since the default [Shell] on almost all Linux distributions is bash(1), this
page lists some common bash-isms that might fail on other sh-type shells
on other Unix/Unix-like [OperatingSystem]s. If there is even the remote possibility that your script might be used by someone other than you, it's
worth putting in a small bit of effort to use more portable constructs.
+
+
+This little section is quoted from the documentation for autoconf(1) (do "$ info autoconf", although personally I hate info pages). This demonstrates how stable the unix programming environment is (don't use "new" features added since 1977!)
+
+----
+Portable Shell Programming
+
+ When writing your own checks, there are some shell-script programming techniques you should avoid in order to make your code portable. The Bourne shell and upward-compatible shells like the Korn shell and Bash have evolved over the years, but to prevent trouble, do not take advantage of features that were added after UNIX version 7, circa 1977 .
+
+ You should not use shell functions, aliases, negated character classes, or other features that are not found in all Bourne-compatible shells; restrict yourself to the lowest common denominator. Even `unset' is not supported by all shells! Also, include a space after the exclamation point in interpreter specifications, like this:
+<verbatim>
+ #! /usr/bin/perl
+</verbatim>
+ If you omit the space before the path, then 4.2BSD based systems (such as DYNIX) will ignore the line, because they interpret `#! /' as a 4-byte magic number. Some old systems have quite small limits on the length of the `#!' line too, for instance 32 bytes (not including the newline) on SunOS 4.
+----
+
+
!!!the test(1) command
Note that test(1) is often a built-in function in some shells.
;-z: tests if the length of STRING is zero
-On bash, a non-existant
variable is treated like "", but on some other shells
+On bash, a non-existent
variable is treated like "", but on some other shells
it is treated like nothing.
!Bash:
(non-portable)
+<verbatim>
$ test -z $NON_EXISTENT_VARIABLE
$ echo $?
0
+</verbatim>
(more portable)
+<verbatim>
$ test -z "$NON_EXISTENT_VARIABLE"
$ echo $?
0
+</verbatim>
!Solaris sh:
+<verbatim>
$ test -z $NON_EXISTENT_VARIABLE
test: argument expected
$ echo $?
1
$ test -z "$NON_EXISTENT_VARIABLE"
$ echo $?
0
+</verbatim>
;-e: FILE exists
This seems to be a [GNU] extension... some shells don't allow -e as an option to test. In most cases, you can use "-r" (for file is readable) instead.
!eg Solaris sh
+<verbatim>
$ test -e filename
test: argument expected
-
+</verbatim>
!!!Variables
!Bash:
(non-portable)
+<verbatim>
$ export VAR1=foo
$ echo $VAR1
foo
+</verbatim>
(more portable)
+<verbatim>
$ VAR2=bar; export VAR2
$ echo $VAR2
bar
+</verbatim>
Solaris sh:
+<verbatim>
$ export VAR1=foo
VAR1=foo is not an indentifier
$ echo $VAR1
$ VAR2=bar; export VAR2
$ echo $VAR2
bar
-
+</verbatim>
----
Part of CategoryProgramming