Skip to Content
Learning Linux Shell Scripting - Second Edition
book

Learning Linux Shell Scripting - Second Edition

by Ganesh Sanjiv Naik
May 2018
Beginner
332 pages
7h 28m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting - Second Edition

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
  

This should produce the following 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 hai bye # Function ex with three arguments ex World # Function ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting

Learning Linux Shell Scripting

Ganesh Sanjiv Naik
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788993197Supplemental Content