how an argument is passed to function
(Source Advanced Bash Scripting Guide)
Code:
output_args_one_per_line() Code:
[a] |
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.
|
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 |
The loop could/should be something like this:
Code:
for arg in "$@"; do |
Quote:
Code:
f() { for x do echo $x; done } |
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) |
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.
|
It wasn't mentioned. Yes, I keep forgetting to do that.
|
Quote:
Here is my blog on bash: https://www.linuxquestions.org/quest...eniuses-35795/ |
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. |