Define Function in Unix/Linux Command Line (E.G. Bash)

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

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; }

How can I run a function from a script in command line?

If the script only defines the functions and does nothing else, you can first execute the script within the context of the current shell using the source or . command and then simply call the function. See help source for more information.

How to define a function on one line

In Bash, { is not automatically recognized as a special/separate token from what's around it. So you need whitespace between { and mv.

Additionally:

  • } needs to be the start of a command; so if it's not on its own line, you need ; to terminate the previous command.
  • It's a best practice to always use double-quotes around any parameter expansion, since otherwise you'll get bizarre behaviors when the parameters include whitespace or special characters.

So:

rmv() { mv "$2/${1##*/}" "${1%/*}" ; }

define a shell function without a shell script in bash?

This isn't a problem with being inside/outside a script, but a problem in how you're compressing your definition down to a one-liner: You need (but are not including) a semicolon before the closing brace.

The following works:

afunc () { echo "i am awesome"; } && export -f afunc

Can I call a function of a shell script from another shell script?

Refactor your second.sh script like this:

func1 {
fun="$1"
book="$2"
printf "func=%s,book=%s\n" "$fun" "$book"
}

func2 {
fun2="$1"
book2="$2"
printf "func2=%s,book2=%s\n" "$fun2" "$book2"
}

And then call these functions from script first.sh like this:

source ./second.sh
func1 love horror
func2 ball mystery

OUTPUT:

func=love,book=horror
func2=ball,book2=mystery

How to call a function in shell Scripting?

You don't specify which shell (there are many), so I am assuming Bourne Shell, that is I think your script starts with:

#!/bin/sh

Please remember to tag future questions with the shell type, as this will help the community answer your question.

You need to define your functions before you call them. Using ():

process_install()
{
echo "Performing process_install() commands, using arguments [${*}]..."
}

process_exit()
{
echo "Performing process_exit() commands, using arguments [${*}]..."
}

Then you can call your functions, just as if you were calling any command:

if [ "$choice" = "true" ]
then
process_install foo bar
elif [ "$choice" = "false" ]
then
process_exit baz qux

You may also wish to check for invalid choices at this juncture...

else
echo "Invalid choice [${choice}]..."
fi

See it run with three different values of ${choice}.

Good luck!

Passing parameters to a Bash function

There are two typical ways of declaring a function. I prefer the second approach.

function function_name {
command...
}

or

function_name () {
command...
}

To call a function with arguments:

function_name "$arg1" "$arg2"

The function refers to passed arguments by their position (not by name), that is $1, $2, and so forth. $0 is the name of the script itself.

Example:

function_name () {
echo "Parameter #1 is $1"
}

Also, you need to call your function after it is declared.

#!/usr/bin/env sh

foo 1 # this will fail because foo has not been declared yet.

foo() {
echo "Parameter #1 is $1"
}

foo 2 # this will work.

Output:

./myScript.sh: line 2: foo: command not found
Parameter #1 is 2

Reference: Advanced Bash-Scripting Guide.

How to create a function in shell script that receives parameters?

function display_value() {
echo "The value is $1"
}

amount=1
display_value $amount
amount=2
display_value $amount

Return value in a Bash function

Although Bash has a return statement, the only thing you can specify with it is the function's own exit status (a value between 0 and 255, 0 meaning "success"). So return is not what you want.

You might want to convert your return statement to an echo statement - that way your function output could be captured using $() braces, which seems to be exactly what you want.

Here is an example:

function fun1(){
echo 34
}

function fun2(){
local res=$(fun1)
echo $res
}

Another way to get the return value (if you just want to return an integer 0-255) is $?.

function fun1(){
return 34
}

function fun2(){
fun1
local res=$?
echo $res
}

Also, note that you can use the return value to use Boolean logic - like fun1 || fun2 will only run fun2 if fun1 returns a non-0 value. The default return value is the exit value of the last statement executed within the function.



Related Topics



Leave a reply



Submit