Hacker News new | ask | show | jobs
by massysett 3841 days 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.
9 comments

Bash 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.

I wrote sh for this very reason http://amoffat.github.io/sh/ This lets you use a programming language, instead of a shell language, to solve programming tasks.
Actually, 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.

Agreed. 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.
It's useful to be able to write functions for your shell. For example, I have a function up, which moves up n dirs.
I wrote this function for my .bashrc as soon as I read this.
(defun eshell/up (n) (dotimes (i n) (cd "..")))
I use pup a lot too, which just prints the dir.
Zsh 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.
I 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?
At one point I had ".." aliased to one directory up, "..." to two, and so forth. For navigating I found that preferable to cd ../../.. etc.

    alias '..=cd ..'
    alias '~=cd "$HOME"'
Yes. 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. :-)
Even 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.
Please list other backwards-compatible interpreted languages installed by default on dozens of operating systems and platforms spanning 26 years.
Hint: 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?

Awk and perl.

I use a lot of bash though.

About 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.
until you run embedded routers and such, where perl is too large and bash/ash/mush rules.
Or you're bootstrapping an install process and perl isn't included, but a shell is.
Perl, it's everywhere, and is a better bash than bash is.
Build 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>.

Possibly, but bash functions can still be quite useful even in a relatively simple script.
Your 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.