[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: CDPATH and shell scripts



On Fri, Jul 03, 2009 at 01:01:41AM +0200, Goswin von Brederlow wrote:
> As a middle ground I wouldn't mind $SHELL to unset CDPATH when it
> switches from an interactive shell to a non-interactive shell, when a
> script with #! $SHELL is executed. That one is just to damn scary.

I don't think that's permitted by SUSv3, and Policy says:

  Scripts may assume that /bin/sh implements the SUSv3 Shell Command
  Language....

> Also why does it output to stdout and not stderr?

According to SUSv3 (and SUSv4) for the cd utility:

  If a non-empty directory name from CDPATH is used, or if cd - is used,
  an absolute pathname of the new working directory shall be written to
  the standard output as follows:
  
  "%s\n", <new directory>

The rationale states that CDPATH was taken from the SysV shell, and it
probably had that behavior.

If you're going to write a /bin/sh script, you need to be aware of the
environment and how it's going to affect your script.  For example, if
you don't want utilities (such as patch) to have POSIX behavior, you
need to unset POSIXLY_CORRECT and _POSIX2_VERSION.  If you don't want
cd to use CDPATH, you need to unset it.  You can probably do something
like

  unset `set | sed -re 's/^([^=]+)=.*$/\1/g' | grep -E '^[A-Za-z0-9]' | grep -vE '^(PS|PATH|TERM)'` 2>/dev/null

to get a "clean" environment.

-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 713 440 7475 | http://crustytoothpaste.ath.cx/~bmc | My opinion only
OpenPGP: RSA v4 4096b 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187

Attachment: signature.asc
Description: Digital signature


Reply to: