Run a Function in a Script from the Command Line on Linux

BASH (Bourne Again Shell) is a Unix shell and command language widely used in Linux and Unix-like operating systems. One powerful feature of BASH is the ability to create and use functions within script files. Functions are reusable blocks of code that perform specific tasks and can be executed from within the script or directly from the command line.

Prerequisites

Before we begin, you will need

  • A Linux system with a command line interface (or SSH access).

  • A script containing a function you want to run (your custom script).

  • The required permissions to execute the script (read/write/execute).

This tutorial will show you how to run a function in a BASH script from the command line on a Linux system. We will create a BASH script with functions and demonstrate various methods to call them from the command line.

Create a BASH Script with a Function

Let's create a BASH script that contains a simple function. Open a text editor and create a new file called hello.sh with the following code

#!/bin/bash
# Define a function called "hello"
hello() {
   echo "Hello, Earth!"
}

# Define a function with parameters
greet() {
   echo "Hello, $1! Welcome to $2."
}

# Call the function only if script is run directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
   hello
fi

Save the file and make it executable using the chmod command

chmod +x hello.sh

Now, run the script to test it

./hello.sh
Hello, Earth!

Execute a Function from Command Line

Method 1: Using bash -c with source

To run a specific function from the command line, use the bash -c command followed by sourcing the script and calling the function

bash -c "source hello.sh; hello"
Hello, Earth!

For functions with parameters

bash -c "source hello.sh; greet 'Alice' 'Linux'"
Hello, Alice! Welcome to Linux.

Method 2: Using the dot (.) command

Alternatively, use the dot command to source the script and call the function

. hello.sh; greet 'Bob' 'Ubuntu'
Hello, Bob! Welcome to Ubuntu.

Method 3: Creating a wrapper script

For more complex scenarios, create a wrapper script that accepts the function name and parameters as command-line arguments

#!/bin/bash
# run_function.sh
source hello.sh
"$@"  # Execute the function with all passed arguments

Make it executable and use it

chmod +x run_function.sh
./run_function.sh greet 'Charlie' 'Debian'

Advanced Function Operations

Functions with Return Values

BASH functions can return values using the return command. The return value is stored in the $? variable

#!/bin/bash
# math.sh
calculate_square() {
   local number=$1
   local result=$((number * number))
   echo $result
   return 0
}

add_numbers() {
   local sum=$(($1 + $2))
   return $sum
}

Call functions and capture results

bash -c "source math.sh; result=\$(calculate_square 5); echo 'Square of 5 is: \$result'"

Debugging Functions

Enable debugging output using set -x to trace function execution

bash -c "set -x; source hello.sh; greet 'Debug' 'Mode'"

Comparison of Methods

Method Use Case Pros Cons
bash -c "source script; function" One-time execution Simple, direct Verbose for repeated use
. script; function Interactive sessions Loads functions into current shell Functions remain in memory
Wrapper script Regular automation Clean, reusable Requires additional script

Best Practices

  • Function isolation Use the conditional check [[ "${BASH_SOURCE[0]}" == "${0}" ]] to prevent automatic execution when sourcing.

  • Error handling Always validate input parameters and handle errors gracefully.

  • Documentation Add comments describing function parameters and return values.

Conclusion

Running functions from BASH scripts via the command line provides flexibility and modularity in shell scripting. The bash -c and dot sourcing methods allow direct function execution, while wrapper scripts offer cleaner automation solutions. Choose the method that best fits your use case and workflow requirements.

Updated on: 2026-03-17T09:01:38+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements