6 ms·
If you are writing functions in Bash, your task is probably sufficiently complex that it would benefit from being written in a language other than Bash.
by massysett 11y ago
If you are writing functions in Bash, your task is probably sufficiently complex that it would benefit from being written in a language other than Bash.
- peterwwillis 11y agoPlease list other backwards-compatible interpreted languages installed by default on dozens of operating systems and platforms spanning 26 years.
- burke 11y agoAwk and perl. I use a lot of bash though.
- ChrisArgyle 11y agoAbout 7 years ago I had a job where we supported almost every Unix all the way back to AT&T SVR3 (including OpenServer, UnixWare, HP-UX, AIX, Linux, OSF/1). With the exception of SVR3 every single one had binary packages for Perl 5.
- ausjke 11y agountil you run embedded routers and such, where perl is too large and bash/ash/mush rules.
- peterwwillis 11y agoOr you're bootstrapping an install process and perl isn't included, but a shell is.
- s_kilk 11y agoPerl, it's everywhere, and is a better bash than bash is.
- deleted 11y ago[deleted]
- massysett 11y agoHint: it's not Bash. That would be sh, which is not really portable--if it were, autoconf wouldn't spend so much effort on portability. So yes, can anyone provide such a list please?
- OJFord 11y agoActually, this is exactly the sort of guide I've been meaning to read, since I'm inclined to take the opposite view. Too often, I think, my task is probably sufficiently simple that it could be a nice portable obviously-a-script Bash script rather than Python/whatever.
- mannykannot 11y agoAgreed. Bash (and similar scripts) are particularly useful in tasks involving filesystem and process management, where they offer capabilities that are more complicated in other languages. Functions are a helpful way to organize your use of those capabilities.
- usefulcat 11y agoPossibly, but bash functions can still be quite useful even in a relatively simple script.
- uxcn 11y agoIt's useful to be able to write functions for your shell. For example, I have a function up, which moves up n dirs.
- cglace 11y agoI wrote this function for my .bashrc as soon as I read this.
- uxcn 11y agoI use pup a lot too, which just prints the dir.
- tux1968 11y agoZsh has quite a nice option "auto_cd". If the first word you type doesn't resolve to a command, it tries to change the current working directory to it instead. So just typing ~ will get you to $HOME, and .. will take you up one.
- lisivka 11y agoalias '..=cd ..' alias '~=cd "$HOME"'
- tux1968 11y agoYes. Although you get it for free with Zsh where it works with any directory you type. Not just the ones you think to make an alias for beforehand. :-)
- SomeCallMeTim 11y agoAt one point I had ".." aliased to one directory up, "..." to two, and so forth. For navigating I found that preferable to cd ../../.. etc.
- samf 11y agoI love this option. I type "/tmp" when I'm about to create some short-lived scratch files. Those saying "you can just alias that" are missing the point. Where do you stop with the aliases?
- voidlogic 11y agoEven if that is true, it doesn't mean you should not cleanup and organize the bash you have "today" and will port "tomorrow". In the meantime your bash will be better organized and more maintainable.
- daenz 11y agoI wrote sh for this very reason http://amoffat.github.io/sh/ http://amoffat.github.io/sh/ This lets you use a programming language, instead of a shell language, to solve programming tasks.
- vive-la-liberte 11y agoBash functions are very useful when you want to manipulate the state of the shell in some way. That is, you write a function for it and stick that function in your .bash_profile. (Unless ofc an alias would be able to perform the job in which case one should just make an alias instead.) Beyond that, I have been in many situations where standalone shell scripts make the most sense for a task and in which case bash functions have helped me organize the code such that it is both more readable and also so that I don't have to repeat sections of code multiple times within a single script. It all comes down to personal experience and preference, I think.
- iblaine 11y agoYour point is valid, that bash should be avoided as processes become increasingly complex. But there is still room for bash. How much room depends on what you are trying to do.
- Alupis 11y agoBuild scripts for various things are often done in bash, and functions are quite helpful there (unified cleanup routine, for example). Just because you can do something in <pet-language-name-here>, doesn't mean it's best done in <pet-language-name-here>.