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


Introduction

BASH (Bourne Again SHell) is a Unix shell and command language that is used frequently in Linux and other Unix-like operating systems. One of the useful features of BASH is the ability to create and use functions within script files. Functions are blocks of code that complete a specific task and can be executed from within the script or 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 first create a simple BASH script that includes a function, and then demonstrate how to call the function from the command line.

Create a BASH Script with a Function

To start, we will create a BASH script that contains a simple function that displays a message on the terminal. 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!"
}
# Call the function
hello

Save the file and make it executable using the chmod command  

$ chmod +x hello.sh

Now, we can run the script using the ./ prefix and the script name 

$ ./hello.sh
Hello, Earth!

As you can see, the script runs and prints the message "Hello, Earth!" to the terminal.

Execute a Function from bash script in the Command Line

To run a function from the command line, we need to use the bash -c command followed by the name of the script file and the name of the function.

For instance, to run the hello function from the hello.sh script, we can use the following command -

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

Alternatively, we can use the. (dot) command to source the script and call the function

$ . hello.sh; hello
Hello, World!

Advanced actions while executing functions from bash script

  • Passing arguments to a function − You can customize the behavior of a function by passing arguments to it. For example, you might want to create a function that takes a username as an input and prints a personalized greeting. You can pass arguments to a function in BASH using variables such as $1, $2, etc., which represent the first, second, etc. arguments passed to the function.

  • Returning values from a function − BASH functions can return values using the return command. For example, you might create a function that calculates the square of a number and returns the result. The return command is followed by the value you want to return, which is stored in the $? variable and can be accessed from the calling script or the command line.

  • Debugging BASH functions − Debugging BASH functions can be difficult, especially for those new to BASH scripting. One way to debug a function is to use the set -x command, which enables debugging output for the script and prints each command before it is executed. You can turn off debugging output with the set +x command. Other tools such as bashdb or bashdbg offer more advanced debugging features such as breakpoints and variable inspection.

Running a Python Script with Command Line Arguments

To run a function in a script from the command line, you will need to specify the name of the script followed by the name of the function you want to run. For example, if you have a script called myscript.py with a function named myFunction, you can run the function from the command line like this

$ python myscript.py myFunction

If the function takes any arguments, you can pass them after the function name, separated by spaces. For example, if the function takes two arguments arg1 and arg2, you can run it like this

$ python myscript.py myFunction arg1 arg2

Here is an example script with a function that takes two arguments and prints their sum

# myscript.py
def myfunction(arg1, arg2):
   print(arg1 + arg2)
myfunction(1, 2)

To run this function from the command line, use the following command

$ python myscript.py myFunction 1 2

This gives 3 as output.

Conclusion

This tutorial showed you how to run a function in a BASH script from the command line on a Linux system. We first created a simple BASH script that included a function, and then demonstrated how to call the function from the command line using the bash -c and . commands. If you have any questions or comments, please leave them in the comments section below.

Updated on: 04-Jan-2023

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements