Passing Arguments to BASH function


Defining a function/procedure in Linux BASH is quite similar to other high-level languages.

functionName() {
   # content goes here
}

However, passing parameters inside the function is somehow different, which is quite shell-style.

functionName() {
   # the first parameter is $1
   # the second parameter is $2
   # and so on. $0 is the script name
}

bash1 Passing Arguments to BASH function batch script beginner implementation technical

If you want to define local variables that has the limited local scope, use the keyword local. Otherwise the vars defined will be accessed outside the function. This is somehow different than Python and PHP that you have to define global in order to access global variables otherwise the vars will be treated as local.

bash2 Passing Arguments to BASH function batch script beginner implementation technical

Happy scripting!

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
213 words
Last Post: Quick Improvements on your TFileStream Access in Delphi
Next Post: Tools on Linux WINE

The Permanent URL is: Passing Arguments to BASH function

Leave a Reply