Penguin
Note: You are viewing an old revision of this page. View the current version.

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 OperatingSystems. 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.

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 it is treated like nothing.

Bash:

(non-portable)

$ test -z $NON_EXISTENT_VARIABLE $ echo $? 0

(more portable)

$ test -z "$NON_EXISTENT_VARIABLE" $ echo $? 0

!Solaris sh
$ test -z $NON_EXISTENT_VARIABLE test: argument expected $ echo $? 1 $ test -z "$NON_EXISTENT_VARIABLE" $ echo $? 0
-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

$ test -e filename test: argument expected

Variables

Bash:

(non-portable)

$ export VAR1=foo $ echo $VAR1 foo

(more portable)

$ VAR2=bar; export VAR2 $ echo $VAR2 bar

Solaris sh
$ export VAR1=foo VAR1=foo is not an indentifier $ echo $VAR1 $ VAR2=bar; export VAR2 $ echo $VAR2 bar

Part of CategoryProgramming

lib/main.php:944: Notice: PageInfo: Cannot find action page

lib/main.php:839: Notice: PageInfo: Unknown action