johnraff wrote:If you declare a variable inside a bash function it will be local to that function.
You won't be able to access it after the function has run.
This is not true. You have to declare it as a local variable with the local keyword.
#!/bin/bash
evalvariable () {
varvar="hello friend"
}
evalvariable
echo $varvar
Will output: hello friend
While
#!/bin/bash
evalvariable () {
local varvar="hello friend"
}
evalvariable
echo $varvar
will output a blank line.
In fact, you don't declare variables in bash at all. You just fill them with content. The local keyword can be used to create a new variable as a local one without overwriting the global variable going by the same name:
#!/bin/bash
HELLO=Hello
function hello {
local HELLO=World
echo $HELLO
}
echo $HELLO
hello
echo $HELLO
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html
johnraff wrote:If you collect the output of a function in a variable with 'var=$(function)' then everything that function echoes will go in that variable.
...including any error messages etc. Super obvious of course...
I did not. That's beyond cool!
Last edited by Awebb (2011-11-10 09:55:27)
I'm so meta, even this acronym