Passing arguments or parameters to functions

In certain situations, we may need to pass arguments or parameters to functions. In such situations, we can pass arguments as follows.

Calling the script with command-line parameters is as follows:

$ name arg1 arg2 arg3 . . .

Let's type a function as follows:

$  hello() { echo "Hello $1, let us be a friend."; }

Call the function in the command line as follows:

$ hello Ganesh

Output:

Hello Ganesh, let us be a friend

Let's write the script function_07.sh. In this script, we pass command-line parameters to the script as well as the function:

#!/bin/bash quit() { exit } ex() { echo $1 $2 $3 } ex Hello hi bye# Function ex with three arguments ex World# Function ex with one argument echo $1# First argument passed ...

Get Learning Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.