I am calling a perl script to calculate size and variation in bash script. Is there a way to return those two values to separated variables in bash, say $SIZE and $VAR. Only know how to return one value.
Asked
Active
Viewed 1,504 times
1
-
You can find some inspiration in http://stackoverflow.com/questions/2488715/idioms-for-returning-multiple-values-in-shell-scripting – Fredrik Pihl Jun 10 '13 at 19:29
3 Answers
7
Instead of returning (which is mainly used for an error/success code from the script), you can print your variables from the perl script, separated by, say, space, and then read them from bash:
#!/usr/bin/perl
$size=1;
$var=2;
print "$size $var\n";
and:
#!/bin/bash
read SIZE VAR <<<$(my_perl_script)
echo size: $SIZE var: $VAR

Sir Athos
- 9,403
- 2
- 22
- 23
-
3I might use `read SIZE VAR < <( my_perl_script )` instead (but only because it is very slightly cleaner looking). – chepner Jun 10 '13 at 19:31
-
1@chepner: I prefer `<<<` to `< <(` because it doesn't create a named pipe. In most cases, the performance hit won't make any difference though. – Sir Athos Jun 10 '13 at 19:38
-
1@MkV: The question specifically asked for a _bash script_. Why not take advantage of the bash features then? – Sir Athos Jun 14 '13 at 16:12
1
you can only do that by evaluating perl's output e.g.:
from perl:
print "SIZE=1 VAR=blah"
then in shell script:
export `your perl script.pl`

Michael Tabolsky
- 3,429
- 2
- 18
- 11