Define function in unix/linux command line (e.g. BASH)

后端 未结 5 1322
时光说笑
时光说笑 2021-01-04 00:21

Sometimes I have a one-liner that I am repeating many times for a particular task, but will likely never use again in the exact same form. It includes a file name that I am

5条回答
  •  旧巷少年郎
    2021-01-04 01:00

    Quoting my answer for a similar question on Ask Ubuntu:

    Functions in bash are essentially named compound commands (or code blocks). From man bash:

    Compound Commands
       A compound command is one of the following:
       ...
       { list; }
              list  is simply executed in the current shell environment.  list
              must be terminated with a newline or semicolon.  This  is  known
              as  a  group  command. 
    
    ...
    Shell Function Definitions
       A shell function is an object that is called like a simple command  and
       executes  a  compound  command with a new set of positional parameters.
       ... [C]ommand is usually a list of commands between { and },  but
       may  be  any command listed under Compound Commands above.
    

    There's no reason given, it's just the syntax.

    Try with a semicolon after wc -l:

    numresults(){ ls "$1"/RealignerTargetCreator | wc -l; }
    

提交回复
热议问题