2 ms·
> bash fixes that Does every shell that is currently in use? I don't know. I have access to some embedded systems with BusyBox; let's try that: # cat foo
by kazinator 3y ago
> bash fixes that
Does every shell that is currently in use? I don't know. I have access to some embedded systems with BusyBox; let's try that:
# cat foo
#!/bin/sh
printf '$0 is %s\n' "$0"
# ./foo
$0 is ./foo
# PATH=. foo
$0 is ./foo
# PATH= foo
$0 is foo
# /bin/sh --help
BusyBox v1.22.1 (2017-03-02 15:41:43 CST) multi-call binary.
Next, on Ubuntu:
$ PATH= foo
$0 is foo
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Apr 18 2020 /bin/sh -> dash
Next. invoking a script by passing it as an argument to a shell:
$ /bin/bash foo
$0 is foo
$ /bin/bash ./foo
$0 is ./foo
$ /bin/bash ././foo
$0 is ././foo
> the behavior of dirname is inconsistent with respect to the root directory
That is true. The result of dirname cannot be combined with a name without a slash, but the slash is already there in the root case, an annoying special case. Scripts often take the directory of $0 in order to combine it with relative paths.
- saurik 3y agoOk, so I see what is going on with PATH, and it was fascinating (at least to me) and I am really glad I asked: it never ever occurred to me to literally put "." or (apparently even more critically) even "" on a PATH, due to the ever-present danger of entering a folder with a trojan horse called "ls" and getting toasted. When I was talking about "." being on my PATH I meant like, running the program from inside /usr/bin. If I put "~/bin" on my path and run foo it gets converted to an absolute /home/saurik/bin/foo, but if I put "." on my PATH and go into the folder... yeah: you managed to pull off leaving me with just "foo" in $0. I still can't use dirname for most of my scripts, though :(. (If it makes you feel any better, I do often use dirname if it's there.)