LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-01-2022, 02:57 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Local function for printf


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"
 }
 
Old 05-01-2022, 04:20 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
1 members found this post helpful.
Old 05-01-2022, 05:02 PM   #3
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by Faki View Post
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.
 
1 members found this post helpful.
Old 05-02-2022, 09:16 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
Quote:
Originally Posted by computersavvy View Post
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.
 
Old 05-02-2022, 09:33 AM   #5
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
agreed
 
  


Reply

Tags
bash, function



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Bash's Built-in printf Function LXer Syndicated Linux News 0 01-16-2019 02:00 PM
[SOLVED] An issue about printf() function NoWeDoR Programming 1 07-15-2015 03:08 AM
[SOLVED] printf $"Hello $var\n" vs. printf "Hello $var\n" -- not a typo. What is it? GrapefruiTgirl Programming 2 10-21-2010 08:21 AM
[SOLVED] why I couldn't find the source code of printf function in glibc source? famsinyi Programming 5 09-21-2009 09:06 AM
How is 'man 3 printf' different from 'man printf' ?? purpleburple Linux - General 3 09-23-2002 12:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration