2

I have functions like this:

foo() {
    /some/script \
      --opt1=val1 \
      --opt2=val2 \
      --opt3=val3
 }

This is brittle if I take out the last option, or add any new option at any line. The failures are relatively silent, when I re-source the function, meaning I've run these wrong without noticing.

I use these functions by editing .projectrc then re-sourcing. Some of these mistakes lead to errors printed but they are relatively hard to notice. Any discrepancy between type <fcn> and the text in .projectrc should be a problem.

The escaped newlines are brittle and cause problems so I wonder a good alternative that lets me write a function not all on one long line without breaking so readily.

1

This has nothing to do specifically with functions - any kind of shell programming that involves long command lines is susceptible.

One helpful practice is to use set -e in your scripts, so that errors don't just spit out an easily missed warning to STDOUT, but stop the script. Another is to add \ to the end of all lines in a long option list, even the last one - it does no harm and makes it a bit easier to edit the option list later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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