Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Generally speaking anytime you start to breakout bash/zsh/sh/csh functions you've already entered a zone where just writing a python/perl script would be the easier and a more maintainable solution in the long term.


Not if I want to actually affect the current shell environment.

For example:

    export MARKPATH=$HOME/.marks
    function jump {
        cd -P $MARKPATH/$1 2> /dev/null || (echo "No such mark: $1" && marks)
    }
    function mark {
        mkdir -p $MARKPATH; ln -s $(pwd) $MARKPATH/$1
    }
    function unmark {
        rm -i $MARKPATH/$1
    }
    function marks {
        ls -l $MARKPATH | sed 's/  / /g' | cut -d' ' -f9- && echo
    }
    _jump()
    {
        local cur=${COMP_WORDS[COMP_CWORD]}
        COMPREPLY=( $(compgen -W "$( ls $MARKPATH )" -- $cur) )
    }
    complete -F _jump jump
This would be much harder to do in a script.


I have a dozen of functions in my bashrc that are basically a bit more sophisticated aliases. Why would I want to load a whole interpreter to run them while bash can easily do that?


> I have a dozen of functions in my bashrc that are basically a bit more sophisticated aliases.

Me too. Looking at them, they all start with something like

    test $# -eq 2 || return
...since in sh/bash you cannot even name your function's arguments or indicate how many you expect to receive.

Speaking of primitiveness, in strict POSIX sh, there's no such thing as local variables in functions!!


Shell functions are useful for much more than scripting. They are like aliases++, letting you add small commands to your shell where aliases don't quite cut it. They let you handle arguments, export environment variables, use conditionals, etc. I have 100 or so that I've added over the years. An interactive shell without functions would not cut it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: