2

I have gotten quite prolific with the use of the aliases, especially with all the different git commands and their order and interdependencies etc. So, I've created a few alias that run more complex scripts.

alias stash='f() { .... }; f'

Over all very straight forward. However, as a bit of a purest in my development style, I like "well formatted" code. The form is sometimes as important as the function. So, with a simple alias:

alias gca='git commit --amend '

I have no problem listing them straight up in the .bash_aliases file. But for some of the multi-command aliases I would sort of like them to be separate.

Typing help I see

function name { COMMANDS ; } or name () { COMMANDS ; }

but the only way I currently comprehend using the function is in the guise of an alias. But given the colorization of git diff --name-status post I commented on, I can incorporate a script in an SH file and for that example I passed data into it via xargs.

But is it possible (likely) to create "functions" via a script, so instead of them being listed as an "alias" they were listed as an actual function stored in an sh file?

hypothetical:

alias stash='f() { if [[ -z $1 ]]; then git stash list; else git stash $1; fi; }; f'

instead a stash.sh file would have:

function stash
{
  if [[ -z $1 ]]; then
     stash list;
  else git stash $1;
  fi;
}

is something like this possible so in both cases I would simply type "stash" at the prompt but how they are defined is quite different?

1

3 Answers 3

2

You could source into your environment a list of needed functions.

Create a file ~/.bash_functions and source it from ~./bashrc.
The same way as ./bash_aliases is sourced:

if [ -f ~/.bash_functions ]; then
    . ~/.bash_functions
fi

Then, define as many (and as complex) functions you wish.
You could define aliases (inside ~/.bash_aliases to keep things in order) for the functions. But that is not really needed as you could call sourced functions directly. By defining:

stash() {
    if [[ -z $1 ]]; then
        stash list;
    else
        git stash $1;
    fi;
}

You could call it simply by stash or stash this, no alias needed.

1
  • Out of curiosity, are the function definitions in bash able to be fully qualified? Like with parameter lists, etc, or is it always $1, $2, etc? Commented Dec 9, 2015 at 15:25
1

I'm puzzled as to why you're stuffing function definitions into an alias. Your stash alias defines a function calls f, and then immediately calls it in such a way that it receives the arguments passed to the alias. For day-to-day usage, this is equivalent to defining a function called stash:

function stash {
  if (($# == 0)); then
     git stash list;
  else
     git stash "$@";
  fi;
}

Compared with what you have in the alias:

  • You can use function stash { … } or stash () { … } to define a function. They're equivalent, except that if you'd already defined an alias called stash, it would be expanded at the time you try to define the function, so the definition wouldn't end up doing what you wanted. When you define a function, either make sure that the function name isn't already defined as an alias, or use the function syntax.
  • I use (($#==0)) to test whether the function has any arguments, rather than testing whether the first argument is empty. The intent is clearer. (And this allows an empty argument, but this isn't meaningful here anyway.)
  • Always use double quotes around variable expansions.
  • I use "$@" rather than "$1" when calling git. "$@" passes all the arguments of the function.

You can put function definitions in the same place as alias definitions: in your .bashrc, in .bash_aliases, or in another file which is sourced by .bashrc, e.g.

. ~/lib/git-functions.bash
1
  • 1
    Ah, if it wasn't clear from my post, I had not found the documentation on "how" to create a simple function and execute it. the only excution method i'd found was through aliases. as I said, i am learning. Thanks. Commented Dec 9, 2015 at 15:19
0
{   cat >myfunction.fn
    .  ./myfunction.fn
    fn  1 2 3 4 5
} <<""
    fn(){
        printf %s\\n "$@"
    }

1
2
3
4
5

The shell's . command will source a named file in the current shell's execution environment. Any functions, variables, aliases or other kinds of shell state defined therein will persist when it returns.

You can do an autoload sort of function definition, for example, by defining a stub function:

fn(){ . ./fn.file && fn "$@"; }

...where the file sourced contains a redefinition of fn():

fn(){
   : a whole new fn
}
: any other init state commands you might like to run

You must log in to answer this question.

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