In Bash, let's consider a function that does nothing but echo
the argument followed by "is an integer".
f () { num="${!1}"; echo $num is an integer; }
number=12
f number
# 12 is an integer
I would like to write on a file a number of commands that uses the function f
and then run these commands in parallel using the function parallel
(GNU).
# Write Commands to the file `Commands.txt`
rm Commands.txt
touch Commands.txt
for i in $(seq 1 5)
do
echo "number=$i; f number" >> Commands.txt
done
With source
everything work fine
source Commands.txt
1 is an integer
2 is an integer
3 is an integer
4 is an integer
5 is an integer
However, when I try to run the commands in parallel it returns that the function f
is not found
parallel :::: Commands.txt
/bin/bash: f: command not found
/bin/bash: f: command not found
/bin/bash: f: command not found
/bin/bash: f: command not found
/bin/bash: f: command not found
Is there a way I can make my function f
available for parallel
without having to define the function at each line of the file Commands.txt
?
Best Answer
You basically have three options:
export -f
(which is a non-POSIX bash feature)Option 1 is probably the easiest to demonstrate, so I'll show that one:
Note that your population of
Commands.txt
is likely in error, you needf "$number"
inCommands.txt
to pass the number, notf number
which would pass the literal string "number". Your script to generate it should doecho "number=$i; f \"\$number\""
(note the escapes, which are important to avoid$number
being interpreted atecho
time, or the"
s terminating the string).