6 ms·
Clever trick. Too bad there's no good way to namespace commands. It would be interesting if you could have, for instance: $ mkdir -p /tmp/bin/my $ printf
by Taywee 4y ago
Clever trick. Too bad there's no good way to namespace commands. It would be interesting if you could have, for instance:
$ mkdir -p /tmp/bin/my
$ printf '%s\n%s\n' '#!/bin/sh' 'echo hello' > /tmp/bin/my/hello
$ chmod 755 /tmp/bin/my/hello
$ PATH="/tmp/bin:$PATH"
$ my/hello
hello
- eurasiantiger 4y agoJust add another comma. Of course, this needs some deeper magic to work :) ,my,hello
- woodruffw 4y agoI don't know if I would actually recommend this, but it works: $ touch foo::bar $ chmod +x foo::bar $ ./foo::bar && echo $? It's an empty file so it does nothing, but most shells' lexers should be smart enough to not try and lex '::'. Edit: Forgot to mention, this also works with bash functions and aliases. I'm not sure if POSIX sh will allow it. You can also do things like this (again, not sure I'd recommend it): $ alias +x='chmod +x'
- jwilk 4y ago> mkdir -p /tmp/bin/my This is not a secure way of using /tmp.
- Taywee 4y agoSure, but that's not the point. It's just an illustrative example, and I wanted a short absolute path that is easily recognizable to the reader. I'll fix it, though, to avoid having an example with bad security practices, though. edit: Or I would fix it, but I guess I can't edit it. Oh well.
- floren 4y agoPlan 9 does that, I'm not sure why it never made it into Unix because I always loved it. Your networking-related commands were in `/bin/ip` and you'd run e.g. `ip/ping 1.1.1.1`. If you created the dir `$home/bin/rc/my` and put scripts in it, they would have been runnable as `my/hello` etc like in your example.
- yellowapple 4y agoPlan 9 also more or less does away with the concept of $PATH in the first place; just union-mount (with `bind`) whatever you want onto `/bin` and call it a day. Yet another thing that would've been great for Unix-likes to pull in (I think some Linuxen can technically do it, but with some limitations).
- lawl 4y agoAll linuxen should be able to do that with overlayfs, I think.
- keyle 4y agono matter which kind of twisted idea someone comes up with related to terminals, there is always a guy who chimes in with "in Plan 9 you can do that...". It makes me smile every time. :)
- theblazehen 4y agoI've tested it and it works on Linux (bash and zsh), at least for your custom commands
- lou1306 4y agoAhem, technically there is one way (don't know if it is "good", though): $ export $MY = /tmp/my $ $MY/hello hello
- layer8 4y agoYou could (ab)use home directories for that. I.e. place your hello into /home/my, and then you can invoke it as ~my/hello. You even get namespace completion for free after the `~`.
- Beltalowda 4y agoIn zsh you can do "hash -d my=/path/to/my" and then you can use "~my/hello" from anywhere, and it doesn't need to be in /home.
- account42 4y ago> and it doesn't need to be in /home. Neither do home directories.
- ianthehenry 4y agozsh can do this if you set the PATH_DIRS option, but if I remember right it doesn't autocomplete deeply nested directories, just one level. I made a little tool that dispatches hierarchical commands in a slightly nicer way, with intelligent autocomplete with command descriptions: https://github.com/ianthehenry/sd https://github.com/ianthehenry/sd
- gpderetta 4y agoWhat about: my hello I.e. only my needs to be in the global namespace and all other commands are resolved by my itself.
- Someone 4y agoI think shells should support that, allowing you to do $ my hello It would mean tools such as git, microk8s and aws wouldn’t have to implement that “look up first argument, check whether it’s a command I know, and if so, run it” on their own, and would have slightly simpler auto complete configs. It also could mean launching fewer processes (for the case where ‘my’ is a script or executable that looks up the location of ‘hello’ and launches it)
- _dain_ 4y agohttps://ianthehenry.com/posts/sd-my-script-directory/ https://ianthehenry.com/posts/sd-my-script-directory/