1

I ran into a strange problem.I am running this example from this https://www.gnu.org/software/parallel/parset.html. But it is not working inside the script file.

parset myarray seq 3 ::: 4 5 6
echo "${myarray[1]}"

I am getting the following error if I ran the script file

Unknown option: myarray
Unknown option: seq
Unknown option: 3
Unknown option: :::
Unknown option: 4
Unknown option: 5
Unknown option: 6
parset only works if it is a function. The function is defined as part of env_parallel.    
Do the below and restart your shell.

But if I use the command directly in a terminal it works.What am I doing wrong here

0

1 Answer 1

2

In short: You need to do what the error message tells you to do.

Longer version: There are two things called parset. The first is a shell script that tells you how to enable the function version. That's the entire purpose of this script, to provide setup instructions for people trying to run parset without having first loaded the function definition (and when the function is defined, it takes precedence over the script, so running parset runs the function instead of the script)

The second is a shell function that actually does the work (why does it have to be a function? Because a function running in a shell can modify its own environment while a child process can not modify the environment of its parent. If it were a script, it would be a child process of the parent shell and unable to do its job). That function needs to be defined in the shell that uses it.

To define the function, you need to source env_parallel.$SHELL in your script before you use the functions it defines. That's probably being done in your shell login startup scripts (e.g. ~/.bash_profile) but not in your non-login startup scripts (e.g. ~/.bashrc), which is why it works from your terminal but not from a script.

In other words, if your script is run with bash as the interpreter and the env_parallel.* scripts are in /usr/bin/, add the following somewhere near the start of your script:

. /usr/bin/env_parallel.bash

IMPORTANT: source the appropriate env_parallel.SHELL for the interpreter you're running your script with. e.g. on my debian system, parallel provides the following:

$ ls -l /usr/bin/env_parallel*
-rwxr-xr-x 1 root root  4749 Aug 29  2021 /usr/bin/env_parallel
-rwxr-xr-x 1 root root 14565 Aug 29  2021 /usr/bin/env_parallel.ash
-rwxr-xr-x 1 root root 13565 Aug 29  2021 /usr/bin/env_parallel.bash
-rwxr-xr-x 1 root root  5377 Aug 29  2021 /usr/bin/env_parallel.csh
-rwxr-xr-x 1 root root 14554 Aug 29  2021 /usr/bin/env_parallel.dash
-rwxr-xr-x 1 root root  6643 Aug 29  2021 /usr/bin/env_parallel.fish
-rwxr-xr-x 1 root root 12595 Aug 29  2021 /usr/bin/env_parallel.ksh
-rwxr-xr-x 1 root root 12626 Aug 29  2021 /usr/bin/env_parallel.mksh
-rwxr-xr-x 1 root root 14754 Aug 29  2021 /usr/bin/env_parallel.sh
-rwxr-xr-x 1 root root  5380 Aug 29  2021 /usr/bin/env_parallel.tcsh
-rwxr-xr-x 1 root root 12604 Aug 29  2021 /usr/bin/env_parallel.zsh

Alternatively, add it to your non-login shell startup script (e.g. ~/.bashrc) so that the parset function is available to scripts run by non-login shells.

See man parset for details.

2
  • 1
    . "$(which env_parallel.bash)" I had to add this to make my code work. My bashrc file contains this line but I am not sure It did not work, Any way it works and thank you @Cas May 13 at 13:52
  • Thank you @cas. It was a wonderful answer May 13 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.