6 ms·
I do something a bit special... Instead of naming my script foobarnator (and hence risking that another foobarnator may exist in the future and may clash with m
by TacticalCoder 4y ago
I do something a bit special... Instead of naming my script foobarnator (and hence risking that another foobarnator may exist in the future and may clash with my script), I name my script foobarnatorToBeAliased.sh. Then in my list of aliases I alias foobarnator to foobarnatorToBeAliased.sh.
The alias itself looks a bit weird in that to pass arguments to an aliased command you need to use a function (in Bash at least AFACIT) but it all works fine.
As aliases aren't sourced when other scripts are running, I'm sure my scripts/commands ain't ever going to clash with anything.
I've been doing that since years.
It also works fine to add "wrappers" to existing commands: for example to add additional input sanitization catching common mistakes I'm making when calling some commands.
- ta8645 4y agoWhat has always been a blocker for me to follow your example, is the desire to use many such scripts from the command lines provided inside an $editor or $email client. There probably is a way to configure both of them to see the aliases, but it's more straightforward to just have everything in the PATH.
- nequo 4y agoYes, it seems cleaner to just put "export PATH=$HOME/bin:$PATH" in $HOME/.bashrc. That avoids collisions with /usr/bin and /usr/local/bin. (Or rather, resolves collisions in favor of your personal bin directory.)
- account42 4y agoYour PATH is inherited by child processes that you start, including scripts that expect the system version of commands that have the same name as your commands.
- rlkf 4y agoPorque no los dos? It seems that function ,foo() { printf "Fooo\n"; } ,foo actually works! (at least in my bash 5.0.17 shell)
- blep_ 4y ago> The alias itself looks a bit weird in that to pass arguments to an aliased command you need to use a function (in Bash at least AFACIT) but it all works fine. No you don't: $ alias say=echo $ say beep beep You do need to do something special the other way around (if you wrap a command in a function it needs to explicitly pass the arguments on).
- tomjakubowski 4y agoAnother advantage of aliases: in zsh at least, autocompletions of the aliased command Just Work (it breaks in wrapper functions)
- deathanatos 4y agoI used to do this, but it doesn't work with things like "watch".
- chrisshroba 4y agoI actually just learned from a different HN thread yesterday [1] that aliases do get expanded if you add this to your shell's rc file: alias watch='watch ' Specifically, if an alias ends with a space, it will cause the subsequent arg to be expanded as well. For example, try running: alias beep=echo watch beep hello # This will fail alias watch='watch ' watch beep hello # Now it works! [1]: https://news.ycombinator.com/item?id=31830879 https://news.ycombinator.com/item?id=31830879
- sfoley 4y agoBrilliant feature, but this is the sort of thing that reinforces the idea that CLIs have awful discoverability compared to GUIs.
- account42 4y agoWhich is why CLIs should come with manuals - there is even a dedicated CLI program to view those (well multiple actually, because standards wouldn't be fun if there was only one).
- Jistern 4y agoI start my aliases with a period so that I have... .foo1,.foo2, .foo3, and so on.
- deleted 4y ago[deleted]