Which which is which? Burn the which!
Kaz Kylheku <643-408-1753@kylheku.com> writes:
[...]
The obvious situation is double quotes. Inside double quotes, parameter[...]
expansion happens, but not tilde expansion (not to mention pathname
expansion (globbing) and perhaps some other things).
So this won't work:
PATH="$PATH:~/bin"
Sorry I don't have details, but it is true nonetheless.
There they are.
In fact it probably will *partially* work. As I mentioned elsethread,
bash expands a leading ~ (or even ~username) in an element of $PATH when executing a command.
But if you run a program from the command line that invokes another
program (say, a C program that calls system()), it won't treat that
element of $PATH the same way.
For this and other reasons, though you *can* have a literal ~ in $PATH
in bash, it's best to avoid it and use $HOME instead.
A literal '$HOME'
won't work at all, but that's less likely to be a problem if your at all aware of how double quotes work in the shell.
I suggest that bash's undocumented behavior is less than helpful.
I'll probably submit a bug report.
On 23.01.2025 23:46, Keith Thompson wrote:
[snip]
For this and other reasons, though you *can* have a literal ~ in $PATH
in bash, it's best to avoid it and use $HOME instead.
Or use it correctly, unquoted and unescaped.
In article <ccr96l-eot.ln1@ID-313840.user.individual.net>,
Geoff Clare <netnews@gclare.org.uk> wrote:
...
Yes. Perhaps I trimmed too much. The post I was replying to said >>"$HOME/bin [..] is better than ~/bin, because tilde expansion is not, >>AFAIK, included in POSIX" and $HOME is also, of course, expanded before >>PATH is assigned. So there is no reason to prefer $HOME/bin over ~/bin >>since (when used in an assignment) they are equivalent in POSIX.
1) I have no idea what your beef with Kaz is. It seems silly at best.
2) I know this isn't going to sit well with you, but there absolutely are situations (in bash) where ~ doesn't work as a substitute for $HOME, when setting the various "path" variables. I know that I had lines in .profile and/or .bashrc like: PATH=~/bin:$PATH (and similar) that did not work (that is, the value stored in the variable contained an explicit tilde rather
than the expanded value - and this, of course, doesn't work at runtime). Replacing the tilde with $HOME fixes this.
Sorry I don't have details, but it is true nonetheless.
On 23.01.2025 17:47, Dan Cross wrote:
[snip]
: term; echo $HOME
/home/cross
: term; pwd
/home/cross
: term; echo echo xyzzy >bin/quux
: term; chmod +x bin/quux
: term; which quux
/home/cross/bin/quux
: term; quux
xyzzy
: term; exec ksh
: term; export PATH=~/bin:/bin:/usr/bin
: term; echo $PATH
/home/cross/bin:/bin:/usr/bin
: term; which quux
/home/cross/bin/quux
: term; quux
xyzzy
: term; export PATH="$HOME/bin:/bin:/usr/bin"
: term; echo $PATH
/home/cross/bin:/bin:/usr/bin
: term; which quux
/home/cross/bin/quux
: term; quux
xyzzy
: term; export PATH="~/bin:/bin:/usr/bin"
: term; echo $PATH
~/bin:/bin:/usr/bin
: term; which quux
: term; quux
ksh: quux: not found
: term; exec /usr/pkg/bin/bash
: term; echo $PATH
~/bin:/bin:/usr/bin
: term; which quux
: term; quux
xyzzy
: term;
Bash behaves strange here; 'which' doesn't find the executable but >nonetheless bash executes it, shows its output?
I've changed the above test frame to remove some unnecessary parts
to eliminate distracting complexity. The output for three shells:
[snip]
I suppose the moral is, for maximal portability, prefer using
$HOME (or another similar variable) to `~` when setting $PATH
unless you're doing so in a context where you are sure that `~`
will be expanded during assignment.
IMO, the consequence is to not quote a tilde expression in the first
place if you want it expanded in a shell variable. *Violating* that
rule will _in Bash_ produce this strange effect (in the given setup).
To me it appears that not finding (by 'which') an executable but
executing it nonetheless qualifies as a bug [in Bash].
As others have already pointed out, it's probably because you quoted the >tilde.
In article <vmu94j$1q2lp$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
Bash behaves strange here; 'which' doesn't find the executable but
nonetheless bash executes it, shows its output?
Bash doesn't behave strangely here at all. Outside of its POSIX
mode, it's free to implement whatever behavior it likes with
respect to `~` expansion, and this is the behavior the author
has chosen.
But, critically, that does not mean that the programs _that it
invokes_ have to do the same. So while `bash` can perform `~`
expansion on the components of $PATH when it's searching for a
program to execute, one shouldn't be surprised if other programs
don't do the same thing.
As was pointed out, `which` is not built into the shell, and
therefore, not obligated to follow `bash`'s expansion rules.
I've changed the above test frame to remove some unnecessary parts
to eliminate distracting complexity. The output for three shells:
Sorry, I think this cuts too much: the point was to show how the
different shells handle `~` in different contexts; the actual
strings that are in `$PATH` are important to show.
[...]
In article <vmvp3d$2671i$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 23.01.2025 23:46, Keith Thompson wrote:
[snip]
For this and other reasons, though you *can* have a literal ~ in $PATH
in bash, it's best to avoid it and use $HOME instead.
Or use it correctly, unquoted and unescaped.
Or just don't use it, and then you don't have to worry about it.
On 2025-01-23, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
Bash behaves strange here; 'which' doesn't find the executable but
nonetheless bash executes it, shows its output?
which is a nonstandard command; the POSIX command is type.
In those systems where "which" exists at all, it is often
a locally brewed program that is not exactly the same like
the one in other systems.
In Debian and derivatives thereof, /usr/bin/which is a shell script.
I see that in MacOS, there is a /usr/bin/which whose --help
says to send mail to which-bugs<at>gnu.org; the man page
implicates a Carlo Wood as the culprit behind it.
It doesn't appear to be part of GNU Coreutils.
Which which is which? Burn the which!
On 24.01.2025 14:46, Dan Cross wrote:
In article <vmu94j$1q2lp$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
Bash behaves strange here; 'which' doesn't find the executable but
nonetheless bash executes it, shows its output?
Bash doesn't behave strangely here at all. Outside of its POSIX
mode, it's free to implement whatever behavior it likes with
respect to `~` expansion, and this is the behavior the author
has chosen.
But, critically, that does not mean that the programs _that it
invokes_ have to do the same. So while `bash` can perform `~`
expansion on the components of $PATH when it's searching for a
program to execute, one shouldn't be surprised if other programs
don't do the same thing.
As I see it, Bash does the same correct assignment to PATH in
case the tilde-expression is quoted or unquoted; here there's
no difference.
Quoted or escaped it's a literal tilde in PATH,
unquoted it's an expanded tilde-expression. (As in all those
shells; bash, ksh, zsh, dash, sh.)
As was pointed out, `which` is not built into the shell, and
therefore, not obligated to follow `bash`'s expansion rules.
And this (at least) seems to be the source of an inconsistency;
'which' is also in other shells not a built-in. But all other
shells I tested (ksh, zsh, dash, sh) handle it consistently;
if 'which' ("/usr/bin/which") detects no program [in PATH] it
should not execute some program. Other shells do that correctly.
I've changed the above test frame to remove some unnecessary parts
to eliminate distracting complexity. The output for three shells:
Sorry, I think this cuts too much: the point was to show how the
different shells handle `~` in different contexts; the actual
strings that are in `$PATH` are important to show.
The actual strings and their handling WRT PATH is actually the
same in all these shells!
Those test-cases have hidden the real problem,
The settings PATH=~/bin and PATH="~/bin" respectively shall
result in the same behavior across shells when searching for
programs; in the first case looking into "/home/someuser/bin/"
and in the second case looking into "./~/bin/" (i.e. a path
component with a local directory named "~").
It appears as if Bash invokes a tilde-expansion twice(!); once
[if unquoted] when the assignment happens, and another time
when the path-search is actually done.
Both of our test-cases
indicated that Bash seems to tilde-expand a PATH variable value
a second time;
so while 'which' (as other shells and programs)
will not find any executable in "./~/bin/" (which would be the
correct interpretation of a quoted "~/bin") Bash does.
That inconsistency - and deviating from all the other shells -,
if not a bug, looks like a Bad Design Idea if it's been done
deliberately. - YMMV.
On 24.01.2025 14:33, Dan Cross wrote:
In article <vmvp3d$2671i$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 23.01.2025 23:46, Keith Thompson wrote:
[snip]
For this and other reasons, though you *can* have a literal ~ in $PATH >>>> in bash, it's best to avoid it and use $HOME instead.
Or use it correctly, unquoted and unescaped.
Or just don't use it, and then you don't have to worry about it.
But as a Ksh (or any non-Bash shell) user I don't have
to worry about it. (Why shall I see any issue with it?)
[snip]
But more importantly; shell programmers shall be well aware
of what quotes mean in shells! They are not just fancy things
or accessories that one may or may not use as one likes. They
have clear semantics and are essential in shell programming.
If you want tilde-expressions expanded _don't quote them_.
It's not much different from file-globbing; don't escape or
quote a '*' (or other globbing meta-characters) if you want
it to become expanded.
The suggestion to "not use" this ~ or that * is misguiding.
Know the shell concepts! - Or know your shell, at least,
with all its inconsistencies and/or (where applicable) bugs.
On 23.01.2025 22:50, Keith Thompson wrote:[...]
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
To me it appears that not finding (by 'which') an executable but
executing it nonetheless qualifies as a bug [in Bash].
I wouldn't call it a bug in bash. Rather, it's a mismatch between bash
and which.
I'm not sure I can follow you what you mean by "a mismatch". Kornshell,
for example, also accesses /usr/bin/which (but it behaves correctly).
On 24.01.2025 14:33, Dan Cross wrote:
In article <vmvp3d$2671i$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 23.01.2025 23:46, Keith Thompson wrote:
[snip]
For this and other reasons, though you *can* have a literal ~ in $PATH >>>> in bash, it's best to avoid it and use $HOME instead.
Or use it correctly, unquoted and unescaped.
Or just don't use it, and then you don't have to worry about it.
But as a Ksh (or any non-Bash shell) user I don't have
to worry about it. (Why shall I see any issue with it?)
(But as I very early already said; I anyway don't use
tilde-expressions in scripts, even as a Ksh user.)
But more importantly; shell programmers shall be well aware
of what quotes mean in shells! They are not just fancy things
or accessories that one may or may not use as one likes. They
have clear semantics and are essential in shell programming.
If you want tilde-expressions expanded _don't quote them_.
It's not much different from file-globbing; don't escape or
quote a '*' (or other globbing meta-characters) if you want
it to become expanded.
The suggestion to "not use" this ~ or that * is misguiding.
Know the shell concepts! - Or know your shell, at least,
with all its inconsistencies and/or (where applicable) bugs.
On 24.01.2025 14:46, Dan Cross wrote:[...]
As was pointed out, `which` is not built into the shell, and
therefore, not obligated to follow `bash`'s expansion rules.
And this (at least) seems to be the source of an inconsistency;
'which' is also in other shells not a built-in. But all other
shells I tested (ksh, zsh, dash, sh) handle it consistently;
if 'which' ("/usr/bin/which") detects no program [in PATH] it
should not execute some program. Other shells do that correctly.
The settings PATH=~/bin and PATH="~/bin" respectively shall
result in the same behavior across shells when searching for
programs; in the first case looking into "/home/someuser/bin/"
and in the second case looking into "./~/bin/" (i.e. a path
component with a local directory named "~").
It appears as if Bash invokes a tilde-expansion twice(!); once
[if unquoted] when the assignment happens, and another time
when the path-search is actually done. Both of our test-cases
indicated that Bash seems to tilde-expand a PATH variable value
a second time; so while 'which' (as other shells and programs)
will not find any executable in "./~/bin/" (which would be the
correct interpretation of a quoted "~/bin") Bash does.
In article <vn0bpf$29qe6$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 24.01.2025 14:46, Dan Cross wrote:
In article <vmu94j$1q2lp$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
Bash behaves strange here; 'which' doesn't find the executable but
nonetheless bash executes it, shows its output?
Bash doesn't behave strangely here at all. Outside of its POSIX
mode, it's free to implement whatever behavior it likes with
respect to `~` expansion, and this is the behavior the author
has chosen.
But, critically, that does not mean that the programs _that it
invokes_ have to do the same. So while `bash` can perform `~`
expansion on the components of $PATH when it's searching for a
program to execute, one shouldn't be surprised if other programs
don't do the same thing.
As I see it, Bash does the same correct assignment to PATH in
case the tilde-expression is quoted or unquoted; here there's
no difference.
Bash demonstrably does not assign the same thing to $PATH when
the `~` is quoted or escaped versus unquoted/unescaped: [...]
As was pointed out, `which` is not built into the shell, and
therefore, not obligated to follow `bash`'s expansion rules.
And this (at least) seems to be the source of an inconsistency;
'which' is also in other shells not a built-in. But all other
shells I tested (ksh, zsh, dash, sh) handle it consistently;
If it's not built into those shells, then those shells don't
"handle" `which` in any meaningful way, other than exec'ing it
as they would any other program.
But `which` was just an example of a program that's not built
into the shell that inspects $PATH, which it inherited from the
shell that invoked it.
The interesting point of the example was to show that, [...]
if 'which' ("/usr/bin/which") detects no program [in PATH] it
should not execute some program. Other shells do that correctly.
Why do you say this? As I wrote earlier, nothing prevents
`bash` from treating $PATH however it likes outside of its POSIX
mode, and its behavior is entirely correct according to the
rules by which it does things. If other shells do things
differently, then that's their behavior; bash need not be bound
by it.
[...]
The actual strings and their handling WRT PATH is actually the
same in all these shells!
No it's not; that was the point.
Those test-cases have hidden the real problem,
That's the thing; there is no "problem." There are some
differences between how shells behave, but they're different
programs, so why is that bad?
The settings PATH=~/bin and PATH="~/bin" respectively shall
result in the same behavior across shells when searching for
programs; in the first case looking into "/home/someuser/bin/"
and in the second case looking into "./~/bin/" (i.e. a path
component with a local directory named "~").
I don't see any reason why that _must_ be true.
Any given shell is free to interpret $PATH any way it choses, [...]
It appears as if Bash invokes a tilde-expansion twice(!); once
[if unquoted] when the assignment happens, and another time
when the path-search is actually done.
Yes. That's precisely what I was showing.
Both of our test-cases
indicated that Bash seems to tilde-expand a PATH variable value
a second time;
I fail to see where this is being done a "second time": it seems
more likly that `bash` doesn't make any special note of the `~`
in `PATH="~/bin:whatever"` until it actuall goes to run a
program.
so while 'which' (as other shells and programs)
will not find any executable in "./~/bin/" (which would be the
correct interpretation of a quoted "~/bin") Bash does.
You seem to have invented a definition of "correct" here and are
pursuing it aggressively, but that is just one definition from
a large set of such definitions; there's no reason to assume the
behavior you expect here is any more correct than what `bash`
does.
That inconsistency - and deviating from all the other shells -,
Careful: "all other shells" is a pretty big net, and it wouldn't
surprise me at all if this broke down if you expanded the set of
things you were looking at. If you really want to twist your
nogging, have a look at what `csh` does, for example.
if not a bug, looks like a Bad Design Idea if it's been done
deliberately. - YMMV.
It's obviously being done deliberately, and is easy to find in
the source code: https://git.savannah.gnu.org/cgit/bash.git/tree/findcmd.c#n533
You may consider it bad design, and you're well within your
rights to do so, but opinions on that vary, and it doesn't mean
yours is correct.
- Dan C.
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
On 24.01.2025 14:46, Dan Cross wrote:[...]
/usr/bin/which is limited in what it can do. It follows POSIX-specified behavior for $PATH; it doesn't recognize any shell-specific rules. [...]
[...]
The settings PATH=~/bin and PATH="~/bin" respectively shall
result in the same behavior across shells when searching for
programs; in the first case looking into "/home/someuser/bin/"
and in the second case looking into "./~/bin/" (i.e. a path
component with a local directory named "~").
What do you mean by "shall result?
All shells that conform to POSIX behave as you describe. bash doesn't conform to POSIX unless you ask it to. Neither do csh, tcsh, and fish.
[...]
BTW, it hadn't occurred to me that you can have a relative path in a component of $PATH, but it does seem to work. I won't be taking
advantage of this information.
In article <vn0cno$29vrs$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 24.01.2025 14:33, Dan Cross wrote:
In article <vmvp3d$2671i$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 23.01.2025 23:46, Keith Thompson wrote:
[snip]
For this and other reasons, though you *can* have a literal ~ in $PATH >>>>> in bash, it's best to avoid it and use $HOME instead.
Or use it correctly, unquoted and unescaped.
Or just don't use it, and then you don't have to worry about it.
But as a Ksh (or any non-Bash shell) user I don't have
to worry about it. (Why shall I see any issue with it?)
Because you might want to put whatever you assign to `PATH`
in quotes, for instance if their are spaces in one of the
component pathnames (people run `bash` on windows and all
kinds of weird places) and the behavior differs. $HOME is
pleasantly boring by comparison.
[snip]
But more importantly; shell programmers shall be well aware
of what quotes mean in shells! They are not just fancy things
or accessories that one may or may not use as one likes. They
have clear semantics and are essential in shell programming.
If you want tilde-expressions expanded _don't quote them_.
What if the expression refers to a file name with a space in
it? Of course, one can escape the whitespace characters in
filenames, but that gets tedious.
~ is equivalent to $HOME in most contexts. I suggest that assuming that
~, like $HOME, is expanded in double quotes is a very easy mistake to
make. And the bash misfeature we're discussing can make it hard to
detect that mistake.
[snip]
You may consider it bad design, and you're well within your
rights to do so, but opinions on that vary, and it doesn't mean
yours is correct.
So you think that behavior of Bash is good design here? - Okay,
noted.
In article <vn0bpf$29qe6$1@dont-email.me>,[...]
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
if not a bug, looks like a Bad Design Idea if it's been done
deliberately. - YMMV.
It's obviously being done deliberately, and is easy to find in
the source code: https://git.savannah.gnu.org/cgit/bash.git/tree/findcmd.c#n533
You may consider it bad design, and you're well within your
rights to do so, but opinions on that vary, and it doesn't mean
yours is correct.
In article <vn2hsj$2pe96$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
[snip]
Most of this discussion seems to be talking at cross-purposes.
I don't see much point in responding to the specifics.
My point was simply to show that shell behavior varies with
respect to how $PATH is treated; caveat emptor.
I don't usually use bash, but if you do and you really don't
like this behavior you can turn it off.
On 24.01.2025 23:00, Keith Thompson wrote:
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
On 24.01.2025 14:46, Dan Cross wrote:[...]
/usr/bin/which is limited in what it can do. It follows POSIX-specified
behavior for $PATH; it doesn't recognize any shell-specific rules. [...]
Sure.
[...]
The settings PATH=~/bin and PATH="~/bin" respectively shall
result in the same behavior across shells when searching for
programs; in the first case looking into "/home/someuser/bin/"
and in the second case looking into "./~/bin/" (i.e. a path
component with a local directory named "~").
What do you mean by "shall result?
I mean that a shell should behave consistently. (I think Bash does
not in the given case.)
All shells that conform to POSIX behave as you describe. bash doesn't
conform to POSIX unless you ask it to. Neither do csh, tcsh, and fish.
We were speaking about shell programming, so [seriously] I don't
consider Csh and Tcsh as sensible sample shells for the discussion.
(Thinking about it, I wonder whether Bash inherited tilde-handling
from Csh, maybe; that would at least explain something.)
(I don't know Fish, so I cannot comment on that.)
[...]
BTW, it hadn't occurred to me that you can have a relative path in a
component of $PATH, but it does seem to work. I won't be taking
advantage of this information.
Yes. Some prefer to add '.' to PATH. (Though I have no intention
to discuss that habit.)
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
On 24.01.2025 23:00, Keith Thompson wrote:
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
On 24.01.2025 14:46, Dan Cross wrote:[...]
/usr/bin/which is limited in what it can do. It follows POSIX-specified >>> behavior for $PATH; it doesn't recognize any shell-specific rules. [...]
Sure.
[...]
The settings PATH=~/bin and PATH="~/bin" respectively shall
result in the same behavior across shells when searching for
programs; in the first case looking into "/home/someuser/bin/"
and in the second case looking into "./~/bin/" (i.e. a path
component with a local directory named "~").
What do you mean by "shall result?
I mean that a shell should behave consistently. (I think Bash does
not in the given case.)
Consistently with what? Bash consistently expands literal '~'s in
$PATH, and consistently disables that expansion in POSIX mode.
All shells have shell-specific features. What's odd about this case is
that bash has a POSIX-violating feature that affects command name
resolution.
[...]
As far as I can tell (I haven't investigated), all Bourne-based
shells other than Bash treat $PATH in the same way when executing
commands. Bash is the only shell I'm aware of that expands literal
'~' in $PATH.
The fact that it's not POSIX compliant behavior is at worst mildly
annoying. Bash correctly disables it in POSIX mode (enabled by the
--posix option, by running "set -o posix", by setting $POSIXLY_CORRECT
before starting bash, or by invoking it sas "sh").
I dislike the behavior because (a) it can quietly cause unexpected
problems (it's easy to miss the fact that $HOME is expanded within
double quotes and ~ is not), and (b) it's not documented. I certainly consider it a misfeature. [...]
[...]
I don't usually use bash, but if you do and you really don't
like this behavior you can turn it off.
[...]
Please don't put words in my mouth. [...]
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
On 24.01.2025 23:00, Keith Thompson wrote:
[...]
I mean that a shell should behave consistently. (I think Bash does
not in the given case.)
Consistently with what? Bash consistently expands literal '~'s in
$PATH, and consistently disables that expansion in POSIX mode.
[...]
It's a feature that (if used) leaks tildes into child processes via
the environment variable. Path resultion in child processes, if it
reaches a PATH element with a tilde, will somehow process that tilde.
I just tried this experiment. I made a directory named ~ and put ~:
as the leading element of PATH. I put a program called "foo" that
directory.
Surely enough, I can run "foo" from the parent directory above.
The exec functions treat ~ as an ordinary path component.
(I cannot do that out of Bash, which processes the tilde, but
the 'p' family of the exec functions will find it!)
This is a problem similar to "." being in PATH.
If someone has, say, "~/bin" in their PATH, ahead of /bin and /usr/bin,
I can put a malicious program in some directory called "~/bin"
somewhere in the filesystem, give that program the name of a common
external utility, and trick the user into changing into that location
where they will run this common command, resolving to my malicious
program.
If we regard this as a security hole, that atually raises the priority
and bolsters the argument that it ought to be removed even if it
breaks some users, perhaps through a process of noisy deprecation.
Furhermore, the case can be made that the exec stuff in the Linux kernel
or C libraries should be patched with a check against components with a leading tilde.
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
So there would, IMO, not be a security hole (i.e. not because of that).
you don't want to miss. - So Bash users, if they don't want to get
bitten in a subtle way, are probably advised to be pointed out to
that behavior, or, as suggested by others already, not to use tilde
at all with PATH in Bash.
On 26.01.2025 06:26, Kaz Kylheku wrote:[...]
If someone has, say, "~/bin" in their PATH, ahead of /bin and /usr/bin,
I can put a malicious program in some directory called "~/bin"
somewhere in the filesystem, give that program the name of a common
external utility, and trick the user into changing into that location
where they will run this common command, resolving to my malicious
program.
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
So there would, IMO, not be a security hole (i.e. not because of that).
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
On Sun, 26 Jan 2025 14:49:16 +0100, Janis Papanagnou wrote:
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
But you can use in a file/directory name.
On Sun, 26 Jan 2025 14:49:16 +0100, Janis Papanagnou wrote:
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
But you can use rCLreorCY in a file/directory name.
3.146 Filename- https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_146
A sequence of bytes consisting of 1 to {NAME_MAX} bytes used to name a
file. The bytes composing the name shall not contain the <NUL> or
<slash> characters.
On Sun, 26 Jan 2025 14:49:16 +0100, Janis Papanagnou wrote:
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
But you can use <line noise> in a file/directory name.
Not in a POSIX-conforming way:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
But you can use rCLreorCY in a file/directory name.
Not in a POSIX-conforming way:
On Mon, 27 Jan 2025 15:48:21 +1100, Alexis wrote:[...]
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
But you can use rCLreorCY in a file/directory name.
Not in a POSIX-conforming way:
ldo@theon:trydir> mkdir f1
ldo@theon:trydir> touch f1/f2
ldo@theon:trydir> touch f1reof2
In article <87msfc7tgq.fsf@gmail.com>, Alexis <flexibeast@gmail.com> wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
But you can use <line noise> in a file/directory name.
Not in a POSIX-conforming way:
Do you know what LDO meant by the above-quoted line noise?
In article <vn2hsj$2pe96$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
[snip]
Most of this discussion seems to be talking at cross-purposes.
I don't see much point in responding to the specifics.
My point was simply to show that shell behavior varies with
respect to how $PATH is treated; caveat emptor.
I don't usually use bash, but if you do and you really don't
like this behavior you can turn it off.
As far as I can tell, you can only turn off the behavior by
running in POSIX mode, which disables a lot of other Bash-specific >functionality. Personally, I'm unwilling to do that. I think I'd
like to see a "set -o" setting that disables just this feature.
You can *avoid* it by being careful not to put literal '~' characters
in $PATH (specifically at the beginning of any element of $PATH).
That's what I do (unintentionally before now, deliberately now that
I know about it.)
On 2025-01-26, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
you don't want to miss. - So Bash users, if they don't want to get
bitten in a subtle way, are probably advised to be pointed out to
that behavior, or, as suggested by others already, not to use tilde
at all with PATH in Bash.
That's a bizarre recommendation. Approximately all uses of tilde
in PATH are variants of PATH=~/bin:$PATH, which are expanded on
assignment in bash as in other sh-type shells.
On 2025-01-26, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
So there would, IMO, not be a security hole (i.e. not because of that).
The / is not part of a name; it is acting as the path component
separator. [...]
*directory* called "~/bin"
On Sun, 26 Jan 2025 14:49:16 +0100, Janis Papanagnou wrote:
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
But you can use rCLreorCY in a file/directory name.
On Sun, 26 Jan 2025 14:49:16 +0100, Janis Papanagnou wrote:
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
But you can use rCLreorCY in a file/directory name.
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
On Mon, 27 Jan 2025 15:48:21 +1100, Alexis wrote:[...]
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
But you can use rCLreorCY in a file/directory name.
Not in a POSIX-conforming way:
ldo@theon:trydir> mkdir f1
ldo@theon:trydir> touch f1/f2
ldo@theon:trydir> touch f1reof2
Yes, yes, we all know what you're saying, and we all hope you've
enjoyed the attention.
Of course the '/' character can appear in a pathname. And of course it cannot appear in a pathname component, which POSIX also calls a
"filename".
On 27.01.2025 01:02, Lawrence D'Oliveiro wrote:
On Sun, 26 Jan 2025 14:49:16 +0100, Janis Papanagnou wrote:
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
But you can use rCLreorCY in a file/directory name.
You can use it in file and directory _paths_ as separator.
On Sun, 26 Jan 2025 23:51:33 -0800, Keith Thompson wrote:
Of course the '/' character can appear in a pathname. And of course it
cannot appear in a pathname component, which POSIX also calls a
"filename".
But you can use rCLreorCY in a file/directory name.
On Sun, 26 Jan 2025 23:51:33 -0800, Keith Thompson wrote:
Of course the '/' character can appear in a pathname. And of course it
cannot appear in a pathname component, which POSIX also calls a
"filename".
But you can use in a file/directory name.
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
On 26.01.2025 06:26, Kaz Kylheku wrote:[...]
If someone has, say, "~/bin" in their PATH, ahead of /bin and /usr/bin,
I can put a malicious program in some directory called "~/bin"
somewhere in the filesystem, give that program the name of a common
external utility, and trick the user into changing into that location
where they will run this common command, resolving to my malicious
program.
To my best knowledge using '/' as part of a file or directory name is
(as the '\0') prohibited by the operating system at a very low level.
Correct, but ...
So there would, IMO, not be a security hole (i.e. not because of that).
It's not a directory named '~/bin'. It's a directory named 'bin'
under a directory named '~'.
Bash interprets '~/bin' as a component of $PATH as $HOME/bin .
Everything(?) else interprets it as a relative path referring to
a bin subdirectory of a literal '~' subdirectory in the current
directory.
Hmm. The exploit Kaz discussed involves programs other than
bash treating '~/bin' as a relative path. But bash itself could
be affected if $HOME expands to a relative path (I've confirmed
the behavior). On the other hand, that's less likely to happen.
Kaz's exploit just requires getting the victim to cd into a specified directory; this would also require getting the user to change the
value of $HOME.
Another interesting tidbit: the GNU `which` command has a "--skip-tilde" option that tells it to skip elements of $PATH that start with '~'
(and also directories anywhere under $HOME).
From the output of `which --help`:
--skip-tilde Skip directories in PATH that start with a tilde.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 59 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 18:04:01 |
| Calls: | 810 |
| Calls today: | 1 |
| Files: | 1,287 |
| D/L today: |
10 files (21,017K bytes) |
| Messages: | 193,396 |