Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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


--- 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?

share|improve this question
Could you elaborate? I'm having a hard time understanding what you're asking. –  Sasha Chedygov Jul 26 '09 at 6:18
@musicfreak: I added my definitions of the given functions to my post. –  Masi Jul 26 '09 at 6:31

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
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. –  Masi Jul 26 '09 at 11:57

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.

share|improve this answer
So the shell function is a function sourced at your PATH such that you can load and call it when needed. –  Masi Jul 26 '09 at 12:11
@Masi, and also a function written in your script file used that is available to be used within it. yes. –  nik Jul 26 '09 at 12:16

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.