3 # This file must be sourced in sh/ash/dash/bash/ksh/mksh/zsh:
6 # source env_parallel.ash
7 # source env_parallel.dash
8 # source env_parallel.bash
9 # source env_parallel.ksh
10 # source env_parallel.mksh
11 # source env_parallel.zsh
13 # after which 'env_parallel' works
16 # Copyright (C) 2016-2025 Ole Tange, http://ole.tange.dk and Free
17 # Software Foundation, Inc.
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 3 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful, but
25 # WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 # General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, see <http://www.gnu.org/licenses/>
31 # or write to the Free Software Foundation, Inc., 51 Franklin St,
32 # Fifth Floor, Boston, MA 02110-1301 USA
34 # SPDX-FileCopyrightText: 2016-2025 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
35 # SPDX-License-Identifier: GPL-3.0-or-later
36 # shellcheck disable=SC2006
39 # env_parallel.{sh,ash,dash,bash,ksh,mksh,zsh}
42 if [ -n "$BASH_VERSION" ]; then
45 _prefix_PARALLEL_ENV
=_prefix_PARALLEL_ENV_bash
46 elif [ -n "$ZSH_VERSION" ]; then
49 _prefix_PARALLEL_ENV
=false
50 elif [ -n "$KSH_VERSION" ]; then
53 _prefix_PARALLEL_ENV
=false
55 # Dash/ash - can these be detected better?
58 _prefix_PARALLEL_ENV
=false
61 _names_of_ALIASES_
$_shell_DIALECT
63 _names_of_ALIASES_sh
() {
64 # alias fails on Unixware 5
65 for _i
in `alias 2>/dev/null | perl -ne 's/^alias //;s/^(\S+)=.*/$1/ && print' 2>/dev/null`; do
66 # Check if this name really is an alias
67 # or just part of a multiline alias definition
68 if alias "$_i" >/dev
/null
2>/dev
/null
; then
73 _names_of_ALIASES_bash
() {
74 # No aliases will return false. This error should be ignored.
75 # shellcheck disable=SC3044
78 _names_of_ALIASES_ksh
() {
79 alias | perl
-pe 's/=.*//'
81 _names_of_ALIASES_zsh
() {
82 # shellcheck disable=SC2086,SC2296
83 print
-l ${(k)aliases}
85 _bodies_of_ALIASES
() {
86 _bodies_of_ALIASES_
$_shell_DIALECT "$@"
88 _bodies_of_ALIASES_sh
() {
90 # myalias='definition' (GNU/Linux ash)
91 # alias myalias='definition' (FreeBSD ash)
92 # so remove 'alias ' from first line
94 echo 'alias '"`alias "$_i" | perl -pe '1..1 and s/^alias //'`"
97 _bodies_of_ALIASES_bash
() {
98 # shellcheck disable=SC3043
101 # shellcheck disable=SC2046
102 if [ $
(alias "$_i" |
wc -l) = 1 ] ; then
103 true Alias is a single line. Good.
105 _warning_PAR
"Alias '$_i' contains newline."
106 _warning_PAR
"Make sure the command has at least one newline after '$_i'."
107 _warning_PAR
"See BUGS in 'man env_parallel'."
112 _bodies_of_ALIASES_ksh
() {
113 alias "$@" | perl
-pe 's/^/alias /;
114 sub warning { print STDERR "env_parallel: Warning: @_\n"; }
115 if(/^alias (\S+)=\$.*\\n/) {
116 warning("Alias \"$1\" contains newline.");
117 warning("Make sure the command has at least one newline after \"$1\".");
118 warning("See BUGS in \"man env_parallel\".");
122 _bodies_of_ALIASES_zsh
() {
123 # shellcheck disable=SC3043
126 echo 'alias '"$(alias "$_i")"
129 _names_of_FUNCTIONS
() {
130 _names_of_FUNCTIONS_
$_shell_DIALECT
132 _names_of_FUNCTIONS_bash
() {
133 # shellcheck disable=SC3044
136 _names_of_maybe_FUNCTIONS
() {
137 set | perl
-ne '/^([A-Z_0-9]+)\s*\(\)\s*\{?$/i and print "$1\n"'
139 _names_of_FUNCTIONS_sh
() {
140 # myfunc is a function
141 # shellcheck disable=SC2046
142 LANG
=C
type `_names_of_maybe_FUNCTIONS` |
143 perl
-ne '/^(\S+) is a function$/ and not $seen{$1}++ and print "$1\n"'
145 _names_of_FUNCTIONS_ksh
() {
146 # shellcheck disable=SC3044
147 typeset
+f | perl
-pe 's/\(\).*//; s/ .*//;'
149 _names_of_FUNCTIONS_zsh
() {
150 # shellcheck disable=SC2086,SC2296
151 print
-l ${(k)functions}
153 _bodies_of_FUNCTIONS
() {
154 _bodies_of_FUNCTIONS_
$_shell_DIALECT "$@"
156 _bodies_of_FUNCTIONS_sh
() {
157 LANG
=C
type "$@" | perl
-ne '/^(\S+) is a function$/ or print'
159 _bodies_of_FUNCTIONS_bash
() {
160 # shellcheck disable=SC3044
163 _bodies_of_FUNCTIONS_ksh
() {
166 _bodies_of_FUNCTIONS_zsh
() {
167 # shellcheck disable=SC3044
170 _names_of_VARIABLES
() {
171 _names_of_VARIABLES_
$_shell_DIALECT
173 _names_of_VARIABLES_sh
() {
174 # This may screw up if variables contain \n and =
175 set | perl
-ne 's/^(\S+?)=.*/$1/ and print;'
177 _names_of_VARIABLES_bash
() {
178 # shellcheck disable=SC3044
181 _names_of_VARIABLES_ksh
() {
183 # perl -pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
185 # ksh: typeset +p | perl -pe 's/^typeset .. //'
186 # shellcheck disable=SC3044
188 perl
-pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
191 _names_of_VARIABLES_zsh
() {
192 # shellcheck disable=SC2086,SC2296
193 print
-l ${(k)parameters}
195 _bodies_of_VARIABLES
() {
196 _bodies_of_VARIABLES_
$_shell_DIALECT "$@"
198 _bodies_of_VARIABLES_sh
() {
202 perl
-e 'print @ARGV' "$_i="
203 eval echo "\"\$$_i\"" | perl
-e '$/=undef; $a=<>; chop($a); print $a' |
204 perl
-pe 's/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\202-\377]/\\$&/go;'"s/'/\\\'/g; s/[\n]/'\\n'/go;";
208 _bodies_of_VARIABLES_bash
() {
209 # shellcheck disable=SC3044
212 _bodies_of_VARIABLES_ksh
() {
213 # shellcheck disable=SC3044
216 _bodies_of_VARIABLES_zsh
() {
217 # shellcheck disable=SC3044
220 _ignore_HARDCODED
() {
221 _ignore_HARDCODED_
$_shell_DIALECT
223 _ignore_HARDCODED_sh
() {
224 # These names cannot be detected
225 echo '(_|TIMEOUT|IFS)'
227 _ignore_HARDCODED_bash
() {
228 # Copying $RANDOM will cause it not to be random
229 # The rest cannot be detected as read-only
230 echo '(RANDOM|_|TIMEOUT|GROUPS|FUNCNAME|DIRSTACK|PIPESTATUS|USERNAME|BASHPID|BASH_[A-Z_]+)'
232 _ignore_HARDCODED_ksh
() {
233 # These names cannot be detected
234 echo '(_|TIMEOUT|IFS)'
236 _ignore_HARDCODED_zsh
() {
237 # These names cannot be detected
238 echo '([-\?\#\!\$\*\@\_0]|zsh_eval_context|ZSH_EVAL_CONTEXT|LINENO|IFS|commands|functions|options|aliases|EUID|EGID|UID|GID|dis_patchars|patchars|terminfo|galiases|keymaps|parameters|jobdirs|dirstack|functrace|funcsourcetrace|zsh_scheduled_events|dis_aliases|dis_reswords|dis_saliases|modules|reswords|saliases|widgets|userdirs|historywords|nameddirs|termcap|dis_builtins|dis_functions|jobtexts|funcfiletrace|dis_galiases|builtins|history|jobstates|funcstack|run-help)'
241 _ignore_READONLY_
$_shell_DIALECT
244 # shellcheck disable=SC1078,SC1079,SC2026
247 # sh on UnixWare: readonly TIMEOUT
248 # ash: readonly var='val
'
250 # mksh: PIPESTATUS[0]
251 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
252 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
253 # zsh: typeset -r var='val
'
254 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
256 $vars = join "|",map { quotemeta $_ } @r;
257 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
260 _ignore_READONLY_sh
() {
261 readonly | _parse_READONLY
263 _ignore_READONLY_bash
() {
264 readonly | _parse_READONLY
266 _ignore_READONLY_ksh
() {
267 readonly | _parse_READONLY
269 _ignore_READONLY_zsh
() {
270 # shellcheck disable=SC3044
271 typeset
-pr | _parse_READONLY
273 _remove_bad_NAMES
() {
274 # Do not transfer vars and funcs from env_parallel
275 # shellcheck disable=SC2006
276 _ignore_RO
="`_ignore_READONLY`"
277 # shellcheck disable=SC2006
278 _ignore_HARD
="`_ignore_HARDCODED`"
279 # To avoid depending on grep dialect, use Perl version of:
280 # grep -Ev '^(...)$' |
286 _bodies_of_FUNCTIONS|
287 _bodies_of_VARIABLES|
298 _list_function_BODIES|
299 _list_variable_VALUES|
304 _names_of_maybe_FUNCTIONS|
306 _prefix_PARALLEL_ENV|
307 _prefix_PARALLEL_ENV_bash|
312 _which_PAR)$/x and next;
313 # Filter names matching --env
314 /^'"$_grep_REGEXP"'$/ or next;
315 /^'"$_ignore_UNDERSCORE"'$/ and next;
316 # Remove readonly variables
317 /^'"$_ignore_RO"'$/ and next;
318 /^'"$_ignore_HARD"'$/ and next;
321 _prefix_PARALLEL_ENV_bash
() {
322 # shellcheck disable=SC3044
324 perl
-pe 's:\s+off:;: and s/^/shopt -u /;
325 s:\s+on:;: and s/^/shopt -s /;
327 echo 'shopt -s expand_aliases &>/dev/null';
330 _get_ignored_VARS
() {
333 $next_is_env and push @envvar, split/,/, $_;
334 $next_is_env=/^--env$/;
336 if(grep { /^_$/ } @envvar) {
337 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
338 print STDERR "parallel: Error: ",
339 "Run \"parallel --record-env\" in a clean environment first.\n";
341 chomp(@ignored_vars = <IN>);
344 if($ENV{PARALLEL_IGNORED_NAMES}) {
345 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
348 $vars = join "|",map { quotemeta $_ } @ignored_vars;
349 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
353 # Get the --env variables if set
354 # --env _ should be ignored
355 # and convert a b c to (a|b|c)
356 # If --env not set: Match everything (.*)
357 _make_grep_REGEXP
() {
360 /^_$/ and $next_is_env = 0;
361 $next_is_env and push @envvar, split/,/, $_;
362 $next_is_env = /^--env$/;
364 $vars = join "|",map { quotemeta $_ } @envvar;
365 print $vars ? "($vars)" : "(.*)";
370 # ll is an alias for ls -l (in ash)
371 # bash is a tracked alias for /bin/bash
372 # true is a shell builtin (in bash)
373 # myfunc is a function (in bash)
374 # myfunc is a shell function (in zsh)
375 # which is /usr/bin/which (in sh, bash)
376 # which is hashed (/usr/bin/which)
377 # gi is aliased to `grep -i' (in bash)
378 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
379 # Return 0 if found, 1 otherwise
381 perl
-pe '$exit += (s/ is an alias for .*// ||
382 s/ is aliased to .*// ||
383 s/ is a function// ||
384 s/ is a shell function// ||
385 s/ is a shell builtin// ||
386 s/.* is hashed .(\S+).$/$1/ ||
387 s/.* is (a tracked alias for )?//);
388 END { exit not $exit }'
391 echo "env_parallel: Warning: $*" >&2
394 echo "env_parallel: Error: $*" >&2
397 if _which_PAR parallel
>/dev
/null
; then
398 true parallel found
in path
400 # shellcheck disable=SC2016
401 _error_PAR
'parallel must be in $PATH.'
405 # Grep regexp for vars given by --env
406 # shellcheck disable=SC2006
407 _grep_REGEXP
="`_make_grep_REGEXP \"$@\"`"
408 unset _make_grep_REGEXP
411 # shellcheck disable=SC2006
412 _ignore_UNDERSCORE
="`_get_ignored_VARS \"$@\"`"
413 unset _get_ignored_VARS
416 if perl
-e 'exit grep { /^--record-?env$/ } @ARGV' -- "$@"; then
421 _names_of_VARIABLES
) |
422 cat > "$HOME"/.parallel
/ignored_vars
427 if perl
-e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
430 # Insert ::: between each level of session
431 # so you can pop off the last ::: at --end-session
432 # shellcheck disable=SC2006
433 PARALLEL_IGNORED_NAMES
="`echo \"$PARALLEL_IGNORED_NAMES\";
437 _names_of_VARIABLES) | perl -ne '
439 map { $ignored_vars{$_}++ }
440 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
444 if(not $ignored_vars{$_}) {
449 export PARALLEL_IGNORED_NAMES
452 if perl
-e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
455 # Pop off last ::: from PARALLEL_IGNORED_NAMES
456 # shellcheck disable=SC2006
457 PARALLEL_IGNORED_NAMES
="`perl -e '
458 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
459 print $ENV{PARALLEL_IGNORED_NAMES}
464 # shellcheck disable=SC2006
465 _alias_NAMES
="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
466 _list_alias_BODIES
="_bodies_of_ALIASES $_alias_NAMES"
467 if [ "$_alias_NAMES" = "" ] ; then
468 # no aliases selected
469 _list_alias_BODIES
="true"
473 # Grep function names
474 # shellcheck disable=SC2006
475 _function_NAMES
="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
476 _list_function_BODIES
="_bodies_of_FUNCTIONS $_function_NAMES"
477 if [ "$_function_NAMES" = "" ] ; then
478 # no functions selected
479 _list_function_BODIES
="true"
481 unset _function_NAMES
483 # Grep variable names
484 # shellcheck disable=SC2006
485 _variable_NAMES
="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
486 _list_variable_VALUES
="_bodies_of_VARIABLES $_variable_NAMES"
487 if [ "$_variable_NAMES" = "" ] ; then
488 # no variables selected
489 _list_variable_VALUES
="true"
491 unset _variable_NAMES
493 if $_eval_needed ; then
494 # shellcheck disable=SC2006
496 eval $_prefix_PARALLEL_ENV;
497 eval $_list_alias_BODIES;
498 eval $_list_function_BODIES;
499 eval $_list_variable_VALUES;
502 # shellcheck disable=SC2006
504 $_prefix_PARALLEL_ENV;
506 $_list_function_BODIES;
507 $_list_variable_VALUES;
511 # Free up some env space
512 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
513 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
514 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
515 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
516 unset _remove_bad_NAMES _grep_REGEXP _parse_READONLY
517 unset _prefix_PARALLEL_ENV
518 unset _ignore_READONLY_sh _ignore_READONLY_bash
519 unset _ignore_READONLY_ksh _ignore_READONLY_zsh
520 unset _ignore_HARDCODED_sh _ignore_HARDCODED_bash
521 unset _ignore_HARDCODED_ksh _ignore_HARDCODED_zsh
522 unset _bodies_of_ALIASES_ksh _bodies_of_ALIASES_sh
523 unset _bodies_of_ALIASES_zsh _bodies_of_FUNCTIONS_bash
524 unset _bodies_of_FUNCTIONS_ksh _bodies_of_FUNCTIONS_sh
525 unset _bodies_of_FUNCTIONS_zsh _bodies_of_VARIABLES_bash
526 unset _bodies_of_VARIABLES_ksh _bodies_of_VARIABLES_sh
527 unset _bodies_of_VARIABLES_zsh
528 unset _names_of_ALIASES _names_of_ALIASES_bash
529 unset _names_of_ALIASES_ksh _names_of_ALIASES_sh
530 unset _names_of_ALIASES_zsh _names_of_FUNCTIONS
531 unset _names_of_FUNCTIONS_bash _names_of_FUNCTIONS_ksh
532 unset _names_of_FUNCTIONS_sh _names_of_FUNCTIONS_zsh
533 unset _names_of_VARIABLES _names_of_VARIABLES_bash
534 unset _names_of_VARIABLES_ksh _names_of_VARIABLES_sh
535 unset _names_of_VARIABLES_zsh _names_of_maybe_FUNCTIONS
537 # Test if environment is too big by running 'true'
538 # shellcheck disable=SC2006,SC2092
539 if `_which_PAR true` >/dev
/null
2>/dev
/null
; then
541 _parallel_exit_CODE
=$?
542 # Clean up variables/functions
544 unset _which_PAR _which_TRUE
545 unset _warning_PAR _error_PAR
546 # Unset _parallel_exit_CODE before return
547 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
550 _error_PAR
"Your environment is too big."
551 _error_PAR
"You can try 3 different approaches:"
552 _error_PAR
"1. Run 'env_parallel --session' before you set"
553 _error_PAR
" variables or define functions."
554 _error_PAR
"2. Use --env and only mention the names to copy."
555 _error_PAR
"3. Try running this in a clean environment once:"
556 _error_PAR
" env_parallel --record-env"
557 _error_PAR
" And then use '--env _'"
558 _error_PAR
"For details see: man env_parallel"
564 _parset_PARALLEL_PRG
=parallel
569 _parset_PARALLEL_PRG
=env_parallel
574 # If $1 contains ',' or space:
575 # Split on , to get the destination variable names
576 # If $1 is a single destination variable name:
577 # Treat it as the name of an array
579 # # Create array named myvar
580 # parset myvar echo ::: {1..10}
583 # # Put output into $var_a $var_b $var_c
584 # varnames=(var_a var_b var_c)
585 # parset "${varnames[*]}" echo ::: {1..3}
588 # # Put output into $var_a4 $var_b4 $var_c4
589 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
593 if [ "$_parset_NAME" = "" ] ; then
594 echo parset
: Error
: No destination variable given.
>&2
595 echo parset
: Error
: Try
: >&2
596 echo parset
: Error
: ' ' parset myarray
echo ::: foo bar
>&2
599 if [ "$_parset_NAME" = "--help" ] ; then
600 echo parset
: Error
: Usage
: >&2
601 echo parset
: Error
: ' ' parset varname GNU Parallel options and
command >&2
606 if [ "$_parset_NAME" = "--version" ] ; then
607 # shellcheck disable=SC2006
608 echo "parset 20250122 (GNU parallel `parallel --minversion 1`)"
609 echo "Copyright (C) 2007-2025 Ole Tange, http://ole.tange.dk and Free Software"
610 echo "Foundation, Inc."
611 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
612 echo "This is free software: you are free to change and redistribute it."
613 echo "GNU parallel comes with no warranty."
615 echo "Web site: https://www.gnu.org/software/parallel"
617 echo "When using programs that use GNU Parallel to process data for publication"
618 echo "please cite as described in 'parallel --citation'."
624 # Bash: declare -A myassoc=( )
625 # Zsh: typeset -A myassoc=( )
626 # Ksh: typeset -A myassoc=( )
627 # shellcheck disable=SC2039,SC2169,SC3044
628 if (typeset
-p "$_parset_NAME" 2>/dev
/null
; echo) |
629 perl
-ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
630 # This is an associative array
631 # shellcheck disable=SC2006
632 eval "`$_parset_PARALLEL_PRG -k --_parset assoc,"$_parset_NAME" "$@
"`"
633 # The eval returns the function!
635 # This is a normal array or a list of variable names
636 # shellcheck disable=SC2006
637 eval "`$_parset_PARALLEL_PRG -k --_parset var,"$_parset_NAME" "$@
"`"
638 # The eval returns the function!