LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2018, 10:46 AM   #1
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 7
Posts: 903

Rep: Reputation: 74
how an argument is passed to function


(Source Advanced Bash Scripting Guide)

Code:
output_args_one_per_line()
{
for arg do
    echo "[$arg]"
  done #  ^    ^   Embed within brackets, for your viewing pleasure.
}
echo; echo "IFS=\" \""
echo "-------"
IFS=" "
var=" a  b c   "
#    ^ ^^   ^^^
output_args_one_per_line $var
This is going to result in:
Code:
[a]
[b]
[c]
Can anyone explain to me how $arg becomes $var? I really don't understand. What if I declared another variable other than 'arg' within the function? How does it know which one to substitute? I don't get it.
 
Old 03-13-2018, 10:59 AM   #2
fatmac
Senior Member
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: AntiX & presently TinyCore - occasionally SliTaz or OpenBSD
Posts: 2,532

Rep: Reputation: Disabled
If I'm reading that correctly, your arg is each of letters in var, because they are separated by your IFS, so it prints them out one at a time as per your do/done statement.
 
1 members found this post helpful.
Old 03-13-2018, 11:34 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,074
Blog Entries: 12

Rep: Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604
Can you provide a link to the guide which refers to this code?

The convention I've always used is that when you call a bash function and give it arguments, that they are the same convention as when you are in a bash script, $1, $2, $3, ... and so forth. Which is to say that if you call the function with some number of arguments, you then reference them using the same numbered convention to name the script passing arguments.

Both of these references support my opinion on this matter:

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html
http://tldp.org/LDP/abs/html/complexfunct.html
 
1 members found this post helpful.
Old 03-13-2018, 11:51 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,558

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
The loop could/should be something like this:
Code:
for arg in "$@"; do
...
done
 
2 members found this post helpful.
Old 03-13-2018, 12:53 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 712Reputation: 712Reputation: 712Reputation: 712Reputation: 712Reputation: 712Reputation: 712
Quote:
Originally Posted by NevemTeve View Post
The loop could/should be something like this:
Code:
for arg in "$@"; do
...
done
bash implictly loops trough $@ when no value is given to for loop. It's cool

Code:
f() { for x do echo $x; done }
f 1 2 3 bla
1
2
3
bla
 
2 members found this post helpful.
Old 03-13-2018, 01:20 PM   #6
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 7
Posts: 903

Original Poster
Rep: Reputation: 74
Indeed, I tested it myself right after I posted iand saw that adding in $@ did the same thing. First I tried $1 and saw that only the first string separated by IFS was display and so on. As you've seen, I was more interested in the behaviour of for var; do than how IFS behaves. I find it strange that it works that way, but I guess I have to take it as it is.

@rtmistler The source is indeed tldp.org: http://tldp.org/LDP/abs/html/internalvariables.html (Example 9-1)
 
Old 03-13-2018, 01:34 PM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,282

Rep: Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390
probably it was already mentioned, use set -xv at the beginning of your script and you will see what's happening. It will force bash to print variables and commands as they really executed.
 
1 members found this post helpful.
Old 03-13-2018, 01:48 PM   #8
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 7
Posts: 903

Original Poster
Rep: Reputation: 74
It wasn't mentioned. Yes, I keep forgetting to do that.
 
Old 03-14-2018, 08:02 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,074
Blog Entries: 12

Rep: Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604
Quote:
Originally Posted by vincix View Post
It wasn't mentioned. Yes, I keep forgetting to do that.
My blog on bash programming contains a lot of debug aids, as well as discusses functions. I do not cover the implicit use of passing arguments. #1 I did not know about that, and #2 I prefer to be clear and explicit with my programming so as to not obfuscate what I've coded. I also feel that is a better way to program, not just my preference, but instead experience with producing software. I've worked for companies which have sold software stacks as source to be used on other platforms and a great deal of our efforts were to ensure we had uniformly written code across all of our modules and followed consistent practices which were also very clear. Otherwise our need to provide support for customers would have been astronomical. It also helped a great deal with testing and validation of new stacks.

Here is my blog on bash: https://www.linuxquestions.org/quest...eniuses-35795/
 
Old 03-14-2018, 08:27 AM   #10
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 7
Posts: 903

Original Poster
Rep: Reputation: 74
I certainly agree with what you're saying, i.e. you should be explicit so that the code is as legible as possible. I'm probably not going to write scripts that way, but it's important that I understand how bash behaves when I come across such scenarios.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Exit function if no arguments passed in kzo81 Linux - Newbie 11 11-26-2016 01:49 PM
[SOLVED] case function for no-argument - default function for script? Batistuta_g_2000 Linux - Newbie 3 03-13-2013 10:00 AM
[SOLVED] Value gets changed when passed to a function in C++ TheIndependentAquarius Programming 7 06-10-2010 03:38 AM
Invalid argument passed to ext2 library while setting up superblock msross Linux - General 10 09-03-2009 02:05 PM
Max. Value that could be passed to an udelay() function ananth86coolguy Linux - Newbie 2 04-20-2009 08:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration