0
1
How can I pass variable from awk to bash? I want to pass a lot of variable, so I don't use:
x=$(awk '.....)
I thing it's not usefull.
0
1
How can I pass variable from awk to bash? I want to pass a lot of variable, so I don't use:
x=$(awk '.....)
I thing it's not usefull.
1
Assuming you trust the incoming data that awk is processing.
You can have awk print out shell variable declarations, and source
the output of awk like it's a shell file:
source <(
awk '
# ....
print "var1=" value1
print "var2=" value2
# ....
' input
)
echo "shell var1 = $var1"
echo "shell var2 = $var2"
0
This is a general problem that has appeared in many variants on StackExchange already, e.g.
The answer is always that it is not possible for a process to change the environment of its parent (and that's what you're trying to achieve when you would ask awk
to set an environment variable in the shell that spawned it).
The solution recommended usually is to source the output, as Glenn has shown. You have to trust the program, though. You're basically executing arbitrary code.
Another, slightly different, solution would be to output the var=value
lines to a named file and source that file, instead. (Sourcing basically executes the contents of a file in the current shell, as if you had typed them; see http://ss64.com/bash/source.html.) An advantage of this approach is that you can check the contents of this file before sourcing it.
It's very strange for me, but it's works. Do you know other way? – diego9403 – 2015-08-31T15:24:18.947
...and assuming the values have no whitespace or metacharacters in them. – rici – 2015-08-31T15:41:40.807
@diego9403, another way would be to not use awk and do whatever you're doing with bash instead. – glenn jackman – 2015-08-31T16:46:40.530