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-2024 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: 2021-2024 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
() {
182 # shellcheck disable=SC3044
184 perl
-pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
187 _names_of_VARIABLES_zsh
() {
188 # shellcheck disable=SC2086,SC2296
189 print
-l ${(k)parameters}
191 _bodies_of_VARIABLES
() {
192 _bodies_of_VARIABLES_
$_shell_DIALECT "$@"
194 _bodies_of_VARIABLES_sh
() {
198 perl
-e 'print @ARGV' "$_i="
199 eval echo "\"\$$_i\"" | perl
-e '$/=undef; $a=<>; chop($a); print $a' |
200 perl
-pe 's/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\202-\377]/\\$&/go;'"s/'/\\\'/g; s/[\n]/'\\n'/go;";
204 _bodies_of_VARIABLES_bash
() {
205 # shellcheck disable=SC3044
208 _bodies_of_VARIABLES_ksh
() {
209 # shellcheck disable=SC3044
212 _bodies_of_VARIABLES_zsh
() {
213 # shellcheck disable=SC3044
216 _ignore_HARDCODED
() {
217 _ignore_HARDCODED_
$_shell_DIALECT
219 _ignore_HARDCODED_sh
() {
220 # These names cannot be detected
221 echo '(_|TIMEOUT|IFS)'
223 _ignore_HARDCODED_bash
() {
224 # Copying $RANDOM will cause it not to be random
225 # The rest cannot be detected as read-only
226 echo '(RANDOM|_|TIMEOUT|GROUPS|FUNCNAME|DIRSTACK|PIPESTATUS|USERNAME|BASHPID|BASH_[A-Z_]+)'
228 _ignore_HARDCODED_ksh
() {
229 # These names cannot be detected
230 echo '(_|TIMEOUT|IFS)'
232 _ignore_HARDCODED_zsh
() {
233 # These names cannot be detected
234 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)'
237 _ignore_READONLY_
$_shell_DIALECT
240 # shellcheck disable=SC1078,SC1079,SC2026
243 # sh on UnixWare: readonly TIMEOUT
244 # ash: readonly var='val
'
246 # mksh: PIPESTATUS[0]
247 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
248 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
249 # zsh: typeset -r var='val
'
250 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
252 $vars = join "|",map { quotemeta $_ } @r;
253 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
256 _ignore_READONLY_sh
() {
257 readonly | _parse_READONLY
259 _ignore_READONLY_bash
() {
260 readonly | _parse_READONLY
262 _ignore_READONLY_ksh
() {
263 readonly | _parse_READONLY
265 _ignore_READONLY_zsh
() {
266 # shellcheck disable=SC3044
267 typeset
-pr | _parse_READONLY
269 _remove_bad_NAMES
() {
270 # Do not transfer vars and funcs from env_parallel
271 # shellcheck disable=SC2006
272 _ignore_RO
="`_ignore_READONLY`"
273 # shellcheck disable=SC2006
274 _ignore_HARD
="`_ignore_HARDCODED`"
275 # To avoid depending on grep dialect, use Perl version of:
276 # grep -Ev '^(...)$' |
282 _bodies_of_FUNCTIONS|
283 _bodies_of_VARIABLES|
294 _list_function_BODIES|
295 _list_variable_VALUES|
300 _names_of_maybe_FUNCTIONS|
302 _prefix_PARALLEL_ENV|
303 _prefix_PARALLEL_ENV_bash|
308 _which_PAR)$/x and next;
309 # Filter names matching --env
310 /^'"$_grep_REGEXP"'$/ or next;
311 /^'"$_ignore_UNDERSCORE"'$/ and next;
312 # Remove readonly variables
313 /^'"$_ignore_RO"'$/ and next;
314 /^'"$_ignore_HARD"'$/ and next;
317 _prefix_PARALLEL_ENV_bash
() {
318 # shellcheck disable=SC3044
320 perl
-pe 's:\s+off:;: and s/^/shopt -u /;
321 s:\s+on:;: and s/^/shopt -s /;
323 echo 'shopt -s expand_aliases &>/dev/null';
326 _get_ignored_VARS
() {
329 $next_is_env and push @envvar, split/,/, $_;
330 $next_is_env=/^--env$/;
332 if(grep { /^_$/ } @envvar) {
333 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
334 print STDERR "parallel: Error: ",
335 "Run \"parallel --record-env\" in a clean environment first.\n";
337 chomp(@ignored_vars = <IN>);
340 if($ENV{PARALLEL_IGNORED_NAMES}) {
341 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
344 $vars = join "|",map { quotemeta $_ } @ignored_vars;
345 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
349 # Get the --env variables if set
350 # --env _ should be ignored
351 # and convert a b c to (a|b|c)
352 # If --env not set: Match everything (.*)
353 _make_grep_REGEXP
() {
356 /^_$/ and $next_is_env = 0;
357 $next_is_env and push @envvar, split/,/, $_;
358 $next_is_env = /^--env$/;
360 $vars = join "|",map { quotemeta $_ } @envvar;
361 print $vars ? "($vars)" : "(.*)";
366 # ll is an alias for ls -l (in ash)
367 # bash is a tracked alias for /bin/bash
368 # true is a shell builtin (in bash)
369 # myfunc is a function (in bash)
370 # myfunc is a shell function (in zsh)
371 # which is /usr/bin/which (in sh, bash)
372 # which is hashed (/usr/bin/which)
373 # gi is aliased to `grep -i' (in bash)
374 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
375 # Return 0 if found, 1 otherwise
377 perl
-pe '$exit += (s/ is an alias for .*// ||
378 s/ is aliased to .*// ||
379 s/ is a function// ||
380 s/ is a shell function// ||
381 s/ is a shell builtin// ||
382 s/.* is hashed .(\S+).$/$1/ ||
383 s/.* is (a tracked alias for )?//);
384 END { exit not $exit }'
387 echo "env_parallel: Warning: $*" >&2
390 echo "env_parallel: Error: $*" >&2
393 if _which_PAR parallel
>/dev
/null
; then
394 true parallel found
in path
396 # shellcheck disable=SC2016
397 _error_PAR
'parallel must be in $PATH.'
401 # Grep regexp for vars given by --env
402 # shellcheck disable=SC2006
403 _grep_REGEXP
="`_make_grep_REGEXP \"$@\"`"
404 unset _make_grep_REGEXP
407 # shellcheck disable=SC2006
408 _ignore_UNDERSCORE
="`_get_ignored_VARS \"$@\"`"
409 unset _get_ignored_VARS
412 if perl
-e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
417 _names_of_VARIABLES
) |
418 cat > "$HOME"/.parallel
/ignored_vars
423 if perl
-e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
426 # Insert ::: between each level of session
427 # so you can pop off the last ::: at --end-session
428 # shellcheck disable=SC2006
429 PARALLEL_IGNORED_NAMES
="`echo \"$PARALLEL_IGNORED_NAMES\";
433 _names_of_VARIABLES) | perl -ne '
435 map { $ignored_vars{$_}++ }
436 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
440 if(not $ignored_vars{$_}) {
445 export PARALLEL_IGNORED_NAMES
448 if perl
-e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
451 # Pop off last ::: from PARALLEL_IGNORED_NAMES
452 # shellcheck disable=SC2006
453 PARALLEL_IGNORED_NAMES
="`perl -e '
454 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
455 print $ENV{PARALLEL_IGNORED_NAMES}
460 # shellcheck disable=SC2006
461 _alias_NAMES
="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
462 _list_alias_BODIES
="_bodies_of_ALIASES $_alias_NAMES"
463 if [ "$_alias_NAMES" = "" ] ; then
464 # no aliases selected
465 _list_alias_BODIES
="true"
469 # Grep function names
470 # shellcheck disable=SC2006
471 _function_NAMES
="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
472 _list_function_BODIES
="_bodies_of_FUNCTIONS $_function_NAMES"
473 if [ "$_function_NAMES" = "" ] ; then
474 # no functions selected
475 _list_function_BODIES
="true"
477 unset _function_NAMES
479 # Grep variable names
480 # shellcheck disable=SC2006
481 _variable_NAMES
="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
482 _list_variable_VALUES
="_bodies_of_VARIABLES $_variable_NAMES"
483 if [ "$_variable_NAMES" = "" ] ; then
484 # no variables selected
485 _list_variable_VALUES
="true"
487 unset _variable_NAMES
489 if $_eval_needed ; then
490 # shellcheck disable=SC2006
492 eval $_prefix_PARALLEL_ENV;
493 eval $_list_alias_BODIES;
494 eval $_list_function_BODIES;
495 eval $_list_variable_VALUES;
498 # shellcheck disable=SC2006
500 $_prefix_PARALLEL_ENV;
502 $_list_function_BODIES;
503 $_list_variable_VALUES;
507 # Free up some env space
508 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
509 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
510 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
511 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
512 unset _remove_bad_NAMES _grep_REGEXP _parse_READONLY
513 unset _prefix_PARALLEL_ENV
514 unset _ignore_READONLY_sh _ignore_READONLY_bash
515 unset _ignore_READONLY_ksh _ignore_READONLY_zsh
516 unset _ignore_HARDCODED_sh _ignore_HARDCODED_bash
517 unset _ignore_HARDCODED_ksh _ignore_HARDCODED_zsh
518 unset _bodies_of_ALIASES_ksh _bodies_of_ALIASES_sh
519 unset _bodies_of_ALIASES_zsh _bodies_of_FUNCTIONS_bash
520 unset _bodies_of_FUNCTIONS_ksh _bodies_of_FUNCTIONS_sh
521 unset _bodies_of_FUNCTIONS_zsh _bodies_of_VARIABLES_bash
522 unset _bodies_of_VARIABLES_ksh _bodies_of_VARIABLES_sh
523 unset _bodies_of_VARIABLES_zsh
524 unset _names_of_ALIASES _names_of_ALIASES_bash
525 unset _names_of_ALIASES_ksh _names_of_ALIASES_sh
526 unset _names_of_ALIASES_zsh _names_of_FUNCTIONS
527 unset _names_of_FUNCTIONS_bash _names_of_FUNCTIONS_ksh
528 unset _names_of_FUNCTIONS_sh _names_of_FUNCTIONS_zsh
529 unset _names_of_VARIABLES _names_of_VARIABLES_bash
530 unset _names_of_VARIABLES_ksh _names_of_VARIABLES_sh
531 unset _names_of_VARIABLES_zsh _names_of_maybe_FUNCTIONS
533 # Test if environment is too big by running 'true'
534 # shellcheck disable=SC2006,SC2092
535 if `_which_PAR true` >/dev
/null
2>/dev
/null
; then
537 _parallel_exit_CODE
=$?
538 # Clean up variables/functions
540 unset _which_PAR _which_TRUE
541 unset _warning_PAR _error_PAR
542 # Unset _parallel_exit_CODE before return
543 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
546 _error_PAR
"Your environment is too big."
547 _error_PAR
"You can try 3 different approaches:"
548 _error_PAR
"1. Run 'env_parallel --session' before you set"
549 _error_PAR
" variables or define functions."
550 _error_PAR
"2. Use --env and only mention the names to copy."
551 _error_PAR
"3. Try running this in a clean environment once:"
552 _error_PAR
" env_parallel --record-env"
553 _error_PAR
" And then use '--env _'"
554 _error_PAR
"For details see: man env_parallel"
560 _parset_PARALLEL_PRG
=parallel
565 _parset_PARALLEL_PRG
=env_parallel
570 # If $1 contains ',' or space:
571 # Split on , to get the destination variable names
572 # If $1 is a single destination variable name:
573 # Treat it as the name of an array
575 # # Create array named myvar
576 # parset myvar echo ::: {1..10}
579 # # Put output into $var_a $var_b $var_c
580 # varnames=(var_a var_b var_c)
581 # parset "${varnames[*]}" echo ::: {1..3}
584 # # Put output into $var_a4 $var_b4 $var_c4
585 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
589 if [ "$_parset_NAME" = "" ] ; then
590 echo parset
: Error
: No destination variable given.
>&2
591 echo parset
: Error
: Try
: >&2
592 echo parset
: Error
: ' ' parset myarray
echo ::: foo bar
>&2
595 if [ "$_parset_NAME" = "--help" ] ; then
596 echo parset
: Error
: Usage
: >&2
597 echo parset
: Error
: ' ' parset varname GNU Parallel options and
command >&2
602 if [ "$_parset_NAME" = "--version" ] ; then
603 # shellcheck disable=SC2006
604 echo "parset 20240822 (GNU parallel `parallel --minversion 1`)"
605 echo "Copyright (C) 2007-2024 Ole Tange, http://ole.tange.dk and Free Software"
606 echo "Foundation, Inc."
607 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
608 echo "This is free software: you are free to change and redistribute it."
609 echo "GNU parallel comes with no warranty."
611 echo "Web site: https://www.gnu.org/software/parallel"
613 echo "When using programs that use GNU Parallel to process data for publication"
614 echo "please cite as described in 'parallel --citation'."
620 # Bash: declare -A myassoc=( )
621 # Zsh: typeset -A myassoc=( )
622 # Ksh: typeset -A myassoc=( )
623 # shellcheck disable=SC2039,SC2169,SC3044
624 if (typeset
-p "$_parset_NAME" 2>/dev
/null
; echo) |
625 perl
-ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
626 # This is an associative array
627 # shellcheck disable=SC2006
628 eval "`$_parset_PARALLEL_PRG -k --_parset assoc,"$_parset_NAME" "$@
"`"
629 # The eval returns the function!
631 # This is a normal array or a list of variable names
632 # shellcheck disable=SC2006
633 eval "`$_parset_PARALLEL_PRG -k --_parset var,"$_parset_NAME" "$@
"`"
634 # The eval returns the function!