Re: Absolute pathnames to commands in shell scripts

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 01/30/04


Date: Fri, 30 Jan 2004 01:45:12 +0100

2004-01-29, 18:35(+01), Stephane CHAZELAS:
[...]
> IFS is not a problem. Depending on the shell/the script there
> may be with ENV, BASH_ENV, FIGNORE, SHELLOPTS, ARGV0, HOME,
> ZDOTDIR, FPATH, LANG, LC_*, TMOUT (funny with bash and ksh93),
> LD_PRELOAD, SHLIB_PATH, LD_LIBRARY_PATH, all sorts of other
> dynamic linker variables, STTY, TMPPREFIX... some of which you
> can't do anything against (as it's too late when the script is
> started).
[...]

some precisions:

IFS:
affects: very early Bourne shells (others ignore the IFS
variable found in there environment on startup)
effect: on those shells, syntax parsing, word splitting...
example:
$ IFS=i sh -c exit
runs "ex" on the "t" file.

ENV:
affects: pdksh, ksh88, zsh in sh or ksh emulation, some shells
based on ash.
effect: sources the given script, command substitution expanded
in ENV value. If the value expands to the path of a fifo, the
shell is blocked.
example:
$ ENV='$(echo foo >&2)' ksh -c :
foo

BASH_ENV:
affects: bash
effect: same as above

FIGNORE:
affects: ksh93
effect: change the filename generation behavior
example:
$ FIGNORE='!(..)' ksh93 -c 'echo rm -rf *.*'
rm -rf ..

SHELLOPTS:
affects: bash
effect: change the shell options
example:
$ SHELLOPTS=noexec:verbose bash -c 'echo foo'
echo foo

ARGV0:
affects: zsh
effect: changes the emulation mode
example:
$ ARGV0=csh zsh -c 'a=/et*; echo "$a"'
/etc

HOME:
affects: zsh
effect: it's the place where ".zshenv" file is found if $ZDOTDIR
is not set.
$ echo echo foo > /tmp/.zshenv
$ HOME=/tmp zsh -c :
foo

ZDOTDIR:
affects: zsh
effect: see above

FPATH:
affects: zsh, ksh
effect: same as PATH except that's for library functions
example:
$ echo echo foo > /tmp/zmv
$ FPATH=/tmp zsh -c 'autoload zmv; zmv a b'
foo

LANG, LC_...:
affects: most modern shells, ksh93 badly
effect: changes the sort order, the charset/language used for
messages, the displayed time format, the "ls -l" output format,
the numeric format (breaks ksh93 script that use floating point
arithmetic)...
example:
$ date +%B
January
$ LC_TIME=fr sh -c '[ "$(date +%B)" = January ] || echo We are not in January'
We are not in January
$ LC_NUMERIC=fr_FR ksh93 -c 'echo $((3.14159))'
ksh93: line 1: 3.14159: arithmetic syntax error

TMOUT:
affects: bash, ksh93
effect: "read" fails if it takes more than $TMOUT to perform
example:
$ TMOUT=1 ksh93 -c '(sleep 2; echo a) | (read a; echo b$a)'
b

TMOUT, PPID, HISTCMD, MAILCHECK, LINENO, OPTIND, RANDOM, SECONDS...
affects: ksh93, pdksh for some
effect: ksh93 returns immediately with an error if the value is
not a valid arithmetic expression.
example:
$ RANDOM=++ ksh93 -c 'echo foo'
ksh93: ++: more tokens expected

LD_PRELOAD, LD_LIBRARY_PATH...
affects: every non statically linked shell
effect: shells rely on functions from libc or other libraries,
they can be replaced by other ones this way. Other side effects,
with other variables, depending on the system.
example:
$ LD_TRACE_LOADED_OBJECTS=1 sh -c :
        libdl.so.2 => /lib/libdl.so.2 (0x40021000)
        libc.so.6 => /lib/libc.so.6 (0x40024000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

STTY:
affects: zsh
effect: change the terminal settings (runs stty before each
command)
example:
$ STTY=-g zsh -c :
500:5:bf:8a3b:3:1c:8:15:4:0:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0

TMPPREFIX:
affects: zsh
effect: change the path where temporary files are created (for
here documents/strings and =(...))
example:
$ TMPPREFIX=/ zsh -c 'cat <<< foo'
zsh: permission denied

TMPDIR:
affects: pdksh
effect: same as above, except that pdksh reverts to the system
default tmp dir if it is unable to create a tmpfile in $TMPDIR
(but it may still fail for file paths too long for instance).

OPTIND:
affects: zsh (a bug)
effect: getopts fails
example:
$ OPTIND=4 zsh -c 'getopts a var -a; echo "<$var>"'
<>

PS4:
affects: most shells
effect: change the display for xtracing, and command
substitution is performed.
$ PS4='$(()' pdksh -cx :
pdksh: no closing quote

EXECSHELL:
affects: pdksh
effects: command used to run command that return ENOEXEC (valid
scripts without a shebang)
example:
$ echo echo b > a; chmod +x a
$ EXECSHELL=echo pdksh -c ./a
./a

POSIXLY_CORRECT:
affects: pdksh,
CDPATH:
affects: most recent shells
effect: cd'ing to a directory may no longer fail, cd may output
unexpected strings.
example:
$ mkdir /tmp/A /tmp/B
$ cd /tmp/B
$ CDPATH=/tmp bash -c 'cd A && pwd'
/tmp/A
/tmp/A

PATH:
affects: every shell
effect: well known

That list is probably not exhaustive.

-- 
Stéphane                      ["Stephane.Chazelas" at "free.fr"]


Relevant Pages

  • Re: Announcing the friendly interactive shell
    ... You can have the same in zsh if you put ... >> terminfo is often not helpfull in that regard. ... but few terminals support that. ... early shells, both type of arguments could even be intersperced ...
    (comp.unix.shell)
  • Re: Shell execution ( [was] Re: Value of $? lost in the beginning of a function.)
    ... This is not related to my problem since I am not running the script ... using ./foo.sh but directly using the proper shell. ... Ie, 'sh foo.sh' containing '#!/bin/sh' being redundant, but 'zsh ... is used to define the interpretor when the file is exec'd. ...
    (freebsd-stable)
  • Re: Shell execution ( [was] Re: Value of $? lost in the beginning of a function.)
    ... This is not related to my problem since I am not running the script ... using ./foo.sh but directly using the proper shell. ... Ie, 'sh foo.sh' containing '#!/bin/sh' being redundant, but 'zsh ... is used to define the interpretor when the file is exec'd. ...
    (freebsd-stable)
  • Re: Shell execution ( [was] Re: Value of $? lost in the beginning of a function.)
    ... This is not related to my problem since I am not running the script ... using ./foo.sh but directly using the proper shell. ... Ie, 'sh foo.sh' containing '#!/bin/sh' being redundant, but 'zsh ... foo.sh' containing '#!/bin/sh' would execute using zsh. ...
    (freebsd-stable)
  • Re: bash vs zsh
    ... A shell script using getoptand "set" to set the args to the script ... >>b) what do I need to do to make zsh give me what I get from bash? ...
    (comp.unix.shell)