3

The current POSIX spec does not specify any options for dot . builtin.

If I do something like:

$ echo 'echo .' > /tmp/-foo
$ PATH=/tmp "$shell" -c '. -foo'

then the result is varied between shells:

  • dash, ash, ksh88, Bourne shell, schily sh, schily osh, heirloom sh work well.
  • bash, yash, ksh93, pdksh, mksh, posh don't. Changing the command to . -- -foo works in these shells.

And also, using -- is a non compliant way, because POSIX spec says that builtin which are not conformed to Utility Syntax Guidelines will ignore --.

zsh is the only shell that works with both cases.

So how can I make . filename work reliably in Bourne-like or POSIX compliant shells?

3

To avoid shell-dependent effects, pass a full path to .. . /absolute/path/to/script and . relative/path/to/script work in all shells.

PATH lookup is rarely useful for sourced scripts anyway. If you do want PATH lookup, then you can do the lookup manually in case the filename starts with -. Or you can require that the filename does not start with -, to keep things simple.

2

I assume this is partly a theoretical question.

In practical terms I'd make it work reliably either by avoiding - as the first character of the included script or by providing a script path (relative or absolute doesn't matter).

Again from a practical perspective, I prefer to code for a known shell, but if that wasn't possible I'd probably end up with an ugly case statement wrapped inside a function. Unless the generalisation had to extend to *csh shells too, in which case I'd probably just run away.

  • It's not theoritical, you can get a filename from user, then source it. – cuonglm Mar 13 '16 at 14:32
  • @cuonglm I'm curious to understand your use case where you would have a filename provided by the user but you didn't know under what shell your script was running. (Ping me on chat if you prefer.) – roaima Mar 13 '16 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.