1

I have been slightly confused about the extraneous use of shell functions.

My definition of a shell function: a function which is in .bashrc such that

.bashrc

--- other default bash configs ----

extract() {     // I call this a shell function
   ....
}

My definition of a function put to PATH: a file which folder is at PATH. For example, we have the file ~/bin/screen/convert.screen

To add the file and other files in the folder to my PATH, I can have the following PATH

 export PATH='/Users/masi/bin/screen:' 

I would like to know when I should put the function as a login shell function and when to my PATH. I prefer the latter at the moment, since the former increases costs of maintenance.

Which is the advantage of an user-made shell function that a function in your PATH does not have?

2
  • 1
    Could you elaborate? I'm having a hard time understanding what you're asking. Jul 26, 2009 at 6:18
  • @musicfreak: I added my definitions of the given functions to my post. Jul 26, 2009 at 6:31

3 Answers 3

4

Shell functions are easily modified and maintained. You should add these to your PATH when you know you will always need them (even if it's much less often), as opposed to a temporary thing. There is no need to load such a large rc file that you keep adding to when you know you won't need everything in it all the time. If there is a large function you use less often but it is very important, make it accessible from your PATH so you can access it only when you need it.

1
  • SO you mean that apps in the PATH are not loaded at login. Their names are instead put to the list which presents programs in the PATH. If you TAB, then this list is searched. --- I have had never such a large shell apps which would slow me computer down. It seems that it is better to add very large shell apps to your PATH if they cause you speed decreases otherwise. Jul 26, 2009 at 11:57
1

It would help if you agreed with the more accepted description of the shell function as a function written in a file (script) that can be sourced to load in to your shell and used in other scripts and shell commands.

If we take this interpretation of the term, then shell functions make your life easier with 'functions' in your scripts and commands.

Going further, you might also consider writing your less frequently used 'functions' in a separate file and storing that with your other scripts. When you need these you could just source that script into your shell.

2
  • So the shell function is a function sourced at your PATH such that you can load and call it when needed. Jul 26, 2009 at 12:11
  • 1
    @Masi, and also a function written in your script file used that is available to be used within it. yes.
    – nik
    Jul 26, 2009 at 12:16
1

I use shell functions for things that are reasonably small and self-contained, and that are helper commands for the shell rather than full programs in their own right. Once something becomes a proper program, I write it up as such and move it to the bin directory in my $HOME folder (which is already in my PATH).

Here's an example:

# Start mpd (if necessary), search for music using mpc, play music.
mp-start()
{
    ps -C mpd > /dev/null || mpd
    mpc search any $1 | mpc add -
    mpc play
}

That's a function not a program. There's very little to it, and I'm only going to use it if I'm already working in the shell.

I also find it cleaner to put all the functions for my shell into a separate file (.bash_functions for me, but I think you are a Z shell user, so adjust accordingly) which I source from my shell's primary rc file. A last tip is to add something like this to your aliases:

alias refunction='vim ~/.bash_functions; source ~/.bash_functions'

That allows you to create a new function on the fly and have it be immediately available once you're done editing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .