LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how an argument is passed to function (https://www.linuxquestions.org/questions/programming-9/how-an-argument-is-passed-to-function-4175625498/)

vincix 03-13-2018 10:46 AM

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.

fatmac 03-13-2018 10:59 AM

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.

rtmistler 03-13-2018 11:34 AM

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

NevemTeve 03-13-2018 11:51 AM

The loop could/should be something like this:
Code:

for arg in "$@"; do
...
done


keefaz 03-13-2018 12:53 PM

Quote:

Originally Posted by NevemTeve (Post 5830560)
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


vincix 03-13-2018 01:20 PM

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)

pan64 03-13-2018 01:34 PM

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.

vincix 03-13-2018 01:48 PM

It wasn't mentioned. Yes, I keep forgetting to do that.

rtmistler 03-14-2018 08:02 AM

Quote:

Originally Posted by vincix (Post 5830628)
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/

vincix 03-14-2018 08:27 AM

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.


All times are GMT -5. The time now is 07:01 AM.