Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have the following function in a bash script. Am using an array to call a shortened version of printf with a colour scheme. Rather than using a variable, I would like to call a function rather than an array variable. How can I do this.
I also want to ask if it is possible have a function inside a bash script that is local to the file. For instance, is it possible to have two functions with the same name, with each file using its corresponding definition of the function on each particular file?
Code:
pc ()
{
local rc="\e[0m";
local bfcode="\033[0;49;33m";
local frmt="${bfcode} %s ${rc}\n";
local _PA=(printf "$frmt");
"${_PA[@]}" " -s, --with-sequence"
}
I do not believe you can perform function overloading in bash.
As far as how to call your function, provided it is in the file or sourced from another script or file, you just call it by name, and include any arguments, if any.
If unsure there are many examples available via a web search for bash functions.
I have the following function in a bash script. Am using an array to call a shortened version of printf with a colour scheme. Rather than using a variable, I would like to call a function rather than an array variable. How can I do this.
I also want to ask if it is possible have a function inside a bash script that is local to the file. For instance, is it possible to have two functions with the same name, with each file using its corresponding definition of the function on each particular file?
Code:
pc ()
{
local rc="\e[0m";
local bfcode="\033[0;49;33m";
local frmt="${bfcode} %s ${rc}\n";
local _PA=(printf "$frmt");
"${_PA[@]}" " -s, --with-sequence"
}
In bash the scope of a variable declared within a function is that function. It is not necessary to declare the variable as local as is the case in some programming languages.
AFAIK it is not possible to overload a function name in bash. This means that you cannot have the function declared twice in the same script environment.
It is possible to use the same function name in a different script but then both scripts cannot be active at the same time.
In bash the scope of a variable declared within a function is that function. It is not necessary to declare the variable as local as is the case in some programming languages.
True, but you do have to explicitly declare that variable in the function in order to limit the scope. If you simply use a variable name within a function, that name has global scope.
Code:
$ cat tryit.sh
#!/bin/bash
function myfunc {
local var1
declare var2
var1=var1-infunc
var2=var2-infunc
var3=var3-infunc
echo "In myfunc: var1=$var1 var2=$var2 var3=$var3"
}
var1=var1-inMain
var2=var2-inMain
var3=var3-inMain
echo "In main: var1=$var1 var2=$var2 var3=$var3"
myfunc
echo "In main: var1=$var1 var2=$var2 var3=$var3"
$ ./tryit
In main: var1=var1-inMain var2=var2-inMain var3=var3-inMain
In myfunc: var1=var1-infunc var2=var2-infunc var3=var3-infunc
In main: var1=var1-inMain var2=var2-inMain var3=var3-infunc
^^^^^^^^^^^^^^^^
As can be seen, changes to the undeclared var3 in the function are seen in the main.
You might as well use the "local" keyword instead of "declare" just to make it obvious what you are doing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.