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.
77 _names_of_ALIASES_ksh
() {
78 alias | perl
-pe 's/=.*//'
80 _names_of_ALIASES_zsh
() {
81 print
-l ${(k)aliases}
83 _bodies_of_ALIASES
() {
84 _bodies_of_ALIASES_
$_shell_DIALECT "$@"
86 _bodies_of_ALIASES_sh
() {
88 # myalias='definition' (GNU/Linux ash)
89 # alias myalias='definition' (FreeBSD ash)
90 # so remove 'alias ' from first line
92 echo 'alias '"`alias "$_i" | perl -pe '1..1 and s/^alias //'`"
95 _bodies_of_ALIASES_bash
() {
98 # shellcheck disable=SC2046
99 if [ $
(alias "$_i" |
wc -l) == 1 ] ; then
100 true Alias is a single line. Good.
102 _warning_PAR
"Alias '$_i' contains newline."
103 _warning_PAR
"Make sure the command has at least one newline after '$_i'."
104 _warning_PAR
"See BUGS in 'man env_parallel'."
109 _bodies_of_ALIASES_ksh
() {
110 alias "$@" | perl
-pe 's/^/alias /;
111 sub warning { print STDERR "env_parallel: Warning: @_\n"; }
112 if(/^alias (\S+)=\$.*\\n/) {
113 warning("Alias \"$1\" contains newline.");
114 warning("Make sure the command has at least one newline after \"$1\".");
115 warning("See BUGS in \"man env_parallel\".");
119 _bodies_of_ALIASES_zsh
() {
122 echo 'alias '"$(alias $_i)"
125 _names_of_FUNCTIONS
() {
126 _names_of_FUNCTIONS_
$_shell_DIALECT
128 _names_of_FUNCTIONS_bash
() {
131 _names_of_maybe_FUNCTIONS
() {
132 set | perl
-ne '/^([A-Z_0-9]+)\s*\(\)\s*\{?$/i and print "$1\n"'
134 _names_of_FUNCTIONS_sh
() {
135 # myfunc is a function
136 # shellcheck disable=SC2046
137 LANG
=C
type `_names_of_maybe_FUNCTIONS` |
138 perl
-ne '/^(\S+) is a function$/ and not $seen{$1}++ and print "$1\n"'
140 _names_of_FUNCTIONS_ksh
() {
142 # ksh = typeset +p -f | perl -pe 's/\(\).*//'
143 typeset
+f | perl
-pe 's/\(\).*//; s/ .*//;'
145 _names_of_FUNCTIONS_zsh
() {
146 print
-l ${(k)functions}
148 _bodies_of_FUNCTIONS
() {
149 _bodies_of_FUNCTIONS_
$_shell_DIALECT "$@"
151 _bodies_of_FUNCTIONS_sh
() {
152 LANG
=C
type "$@" | perl
-ne '/^(\S+) is a function$/ or print'
154 _bodies_of_FUNCTIONS_bash
() {
157 _bodies_of_FUNCTIONS_ksh
() {
160 _bodies_of_FUNCTIONS_zsh
() {
163 _names_of_VARIABLES
() {
164 _names_of_VARIABLES_
$_shell_DIALECT
166 _names_of_VARIABLES_sh
() {
167 # This may screw up if variables contain \n and =
168 set | perl
-ne 's/^(\S+?)=.*/$1/ and print;'
170 _names_of_VARIABLES_bash
() {
173 _names_of_VARIABLES_ksh
() {
175 # perl -pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
177 # ksh: typeset +p | perl -pe 's/^typeset .. //'
179 perl
-pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
182 _names_of_VARIABLES_zsh
() {
183 print
-l ${(k)parameters}
185 _bodies_of_VARIABLES
() {
186 _bodies_of_VARIABLES_
$_shell_DIALECT "$@"
188 _bodies_of_VARIABLES_sh
() {
192 perl
-e 'print @ARGV' "$_i="
193 eval echo "\"\$$_i\"" | perl
-e '$/=undef; $a=<>; chop($a); print $a' |
194 perl
-pe 's/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\202-\377]/\\$&/go;'"s/'/\\\'/g; s/[\n]/'\\n'/go;";
198 _bodies_of_VARIABLES_bash
() {
201 _bodies_of_VARIABLES_ksh
() {
204 _bodies_of_VARIABLES_zsh
() {
207 _ignore_HARDCODED
() {
208 _ignore_HARDCODED_
$_shell_DIALECT
210 _ignore_HARDCODED_sh
() {
211 # These names cannot be detected
212 echo '(_|TIMEOUT|IFS)'
214 _ignore_HARDCODED_bash
() {
215 # Copying $RANDOM will cause it not to be random
216 # The rest cannot be detected as read-only
217 echo '(RANDOM|_|TIMEOUT|GROUPS|FUNCNAME|DIRSTACK|PIPESTATUS|USERNAME|BASHPID|BASH_[A-Z_]+)'
219 _ignore_HARDCODED_ksh
() {
220 # These names cannot be detected
221 echo '(_|TIMEOUT|IFS)'
223 _ignore_HARDCODED_zsh
() {
224 # These names cannot be detected
225 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)'
228 _ignore_READONLY_
$_shell_DIALECT
231 # shellcheck disable=SC1078,SC1079,SC2026
234 # sh on UnixWare: readonly TIMEOUT
235 # ash: readonly var='val
'
237 # mksh: PIPESTATUS[0]
238 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
239 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
240 # zsh: typeset -r var='val
'
241 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
243 $vars = join "|",map { quotemeta $_ } @r;
244 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
247 _ignore_READONLY_sh
() {
248 readonly | _parse_READONLY
250 _ignore_READONLY_bash
() {
251 readonly | _parse_READONLY
253 _ignore_READONLY_ksh
() {
254 readonly | _parse_READONLY
256 _ignore_READONLY_zsh
() {
257 typeset
-pr | _parse_READONLY
259 _remove_bad_NAMES
() {
260 # Do not transfer vars and funcs from env_parallel
261 # shellcheck disable=SC2006
262 _ignore_RO
="`_ignore_READONLY`"
263 # shellcheck disable=SC2006
264 _ignore_HARD
="`_ignore_HARDCODED`"
265 # To avoid depending on grep dialect, use Perl version of:
266 # grep -Ev '^(...)$' |
272 _bodies_of_FUNCTIONS|
273 _bodies_of_VARIABLES|
284 _list_function_BODIES|
285 _list_variable_VALUES|
290 _names_of_maybe_FUNCTIONS|
292 _prefix_PARALLEL_ENV|
293 _prefix_PARALLEL_ENV|
298 _which_PAR)$/x and next;
299 # Filter names matching --env
300 /^'"$_grep_REGEXP"'$/ or next;
301 /^'"$_ignore_UNDERSCORE"'$/ and next;
302 # Remove readonly variables
303 /^'"$_ignore_RO"'$/ and next;
304 /^'"$_ignore_HARD"'$/ and next;
307 _prefix_PARALLEL_ENV_bash
() {
309 perl
-pe 's:\s+off:;: and s/^/shopt -u /;
310 s:\s+on:;: and s/^/shopt -s /;
312 echo 'shopt -s expand_aliases &>/dev/null';
315 _get_ignored_VARS
() {
318 $next_is_env and push @envvar, split/,/, $_;
319 $next_is_env=/^--env$/;
321 if(grep { /^_$/ } @envvar) {
322 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
323 print STDERR "parallel: Error: ",
324 "Run \"parallel --record-env\" in a clean environment first.\n";
326 chomp(@ignored_vars = <IN>);
329 if($ENV{PARALLEL_IGNORED_NAMES}) {
330 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
333 $vars = join "|",map { quotemeta $_ } @ignored_vars;
334 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
338 # Get the --env variables if set
339 # --env _ should be ignored
340 # and convert a b c to (a|b|c)
341 # If --env not set: Match everything (.*)
342 _make_grep_REGEXP
() {
345 /^_$/ and $next_is_env = 0;
346 $next_is_env and push @envvar, split/,/, $_;
347 $next_is_env = /^--env$/;
349 $vars = join "|",map { quotemeta $_ } @envvar;
350 print $vars ? "($vars)" : "(.*)";
355 # ll is an alias for ls -l (in ash)
356 # bash is a tracked alias for /bin/bash
357 # true is a shell builtin (in bash)
358 # myfunc is a function (in bash)
359 # myfunc is a shell function (in zsh)
360 # which is /usr/bin/which (in sh, bash)
361 # which is hashed (/usr/bin/which)
362 # gi is aliased to `grep -i' (in bash)
363 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
364 # Return 0 if found, 1 otherwise
366 perl
-pe '$exit += (s/ is an alias for .*// ||
367 s/ is aliased to .*// ||
368 s/ is a function// ||
369 s/ is a shell function// ||
370 s/ is a shell builtin// ||
371 s/.* is hashed .(\S+).$/$1/ ||
372 s/.* is (a tracked alias for )?//);
373 END { exit not $exit }'
376 echo "env_parallel: Warning: $*" >&2
379 echo "env_parallel: Error: $*" >&2
382 if _which_PAR parallel
>/dev
/null
; then
383 true parallel found
in path
385 # shellcheck disable=SC2016
386 _error_PAR
'parallel must be in $PATH.'
390 # Grep regexp for vars given by --env
391 # shellcheck disable=SC2006
392 _grep_REGEXP
="`_make_grep_REGEXP \"$@\"`"
393 unset _make_grep_REGEXP
396 # shellcheck disable=SC2006
397 _ignore_UNDERSCORE
="`_get_ignored_VARS \"$@\"`"
398 unset _get_ignored_VARS
401 if perl
-e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
406 _names_of_VARIABLES
) |
407 cat > "$HOME"/.parallel
/ignored_vars
412 if perl
-e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
415 # Insert ::: between each level of session
416 # so you can pop off the last ::: at --end-session
417 # shellcheck disable=SC2006
418 PARALLEL_IGNORED_NAMES
="`echo \"$PARALLEL_IGNORED_NAMES\";
422 _names_of_VARIABLES) | perl -ne '
424 map { $ignored_vars{$_}++ }
425 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
429 if(not $ignored_vars{$_}) {
434 export PARALLEL_IGNORED_NAMES
437 if perl
-e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
440 # Pop off last ::: from PARALLEL_IGNORED_NAMES
441 # shellcheck disable=SC2006
442 PARALLEL_IGNORED_NAMES
="`perl -e '
443 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
444 print $ENV{PARALLEL_IGNORED_NAMES}
449 # shellcheck disable=SC2006
450 _alias_NAMES
="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
451 _list_alias_BODIES
="_bodies_of_ALIASES $_alias_NAMES"
452 if [ "$_alias_NAMES" = "" ] ; then
453 # no aliases selected
454 _list_alias_BODIES
="true"
458 # Grep function names
459 # shellcheck disable=SC2006
460 _function_NAMES
="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
461 _list_function_BODIES
="_bodies_of_FUNCTIONS $_function_NAMES"
462 if [ "$_function_NAMES" = "" ] ; then
463 # no functions selected
464 _list_function_BODIES
="true"
466 unset _function_NAMES
468 # Grep variable names
469 # shellcheck disable=SC2006
470 _variable_NAMES
="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
471 _list_variable_VALUES
="_bodies_of_VARIABLES $_variable_NAMES"
472 if [ "$_variable_NAMES" = "" ] ; then
473 # no variables selected
474 _list_variable_VALUES
="true"
476 unset _variable_NAMES
478 if $eval_needed ; then
479 # shellcheck disable=SC2006
481 eval $_list_alias_BODIES;
482 eval $_list_function_BODIES;
483 eval $_list_variable_VALUES;
486 # shellcheck disable=SC2006
488 $_prefix_PARALLEL_ENV;
490 $_list_function_BODIES;
491 $_list_variable_VALUES;
495 # Free up some env space
496 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
497 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
498 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
499 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
500 unset _remove_bad_NAMES _grep_REGEXP
501 unset _prefix_PARALLEL_ENV
502 # Test if environment is too big by running 'true'
503 # shellcheck disable=SC2006,SC2092
504 if `_which_PAR true` >/dev
/null
2>/dev
/null
; then
506 _parallel_exit_CODE
=$?
507 # Clean up variables/functions
509 unset _which_PAR _which_TRUE
510 unset _warning_PAR _error_PAR
511 # Unset _parallel_exit_CODE before return
512 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
515 _error_PAR
"Your environment is too big."
516 _error_PAR
"You can try 3 different approaches:"
517 _error_PAR
"1. Run 'env_parallel --session' before you set"
518 _error_PAR
" variables or define functions."
519 _error_PAR
"2. Use --env and only mention the names to copy."
520 _error_PAR
"3. Try running this in a clean environment once:"
521 _error_PAR
" env_parallel --record-env"
522 _error_PAR
" And then use '--env _'"
523 _error_PAR
"For details see: man env_parallel"
529 _parset_PARALLEL_PRG
=parallel
534 _parset_PARALLEL_PRG
=env_parallel
539 # If $1 contains ',' or space:
540 # Split on , to get the destination variable names
541 # If $1 is a single destination variable name:
542 # Treat it as the name of an array
544 # # Create array named myvar
545 # parset myvar echo ::: {1..10}
548 # # Put output into $var_a $var_b $var_c
549 # varnames=(var_a var_b var_c)
550 # parset "${varnames[*]}" echo ::: {1..3}
553 # # Put output into $var_a4 $var_b4 $var_c4
554 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
558 if [ "$_parset_NAME" = "" ] ; then
559 echo parset
: Error
: No destination variable given.
>&2
560 echo parset
: Error
: Try
: >&2
561 echo parset
: Error
: ' ' parset myarray
echo ::: foo bar
>&2
564 if [ "$_parset_NAME" = "--help" ] ; then
565 echo parset
: Error
: Usage
: >&2
566 echo parset
: Error
: ' ' parset varname GNU Parallel options and
command >&2
571 if [ "$_parset_NAME" = "--version" ] ; then
572 # shellcheck disable=SC2006
573 echo "parset 20240522 (GNU parallel `parallel --minversion 1`)"
574 echo "Copyright (C) 2007-2024 Ole Tange, http://ole.tange.dk and Free Software"
575 echo "Foundation, Inc."
576 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
577 echo "This is free software: you are free to change and redistribute it."
578 echo "GNU parallel comes with no warranty."
580 echo "Web site: https://www.gnu.org/software/parallel"
582 echo "When using programs that use GNU Parallel to process data for publication"
583 echo "please cite as described in 'parallel --citation'."
589 # Bash: declare -A myassoc=( )
590 # Zsh: typeset -A myassoc=( )
591 # Ksh: typeset -A myassoc=( )
592 # shellcheck disable=SC2039,SC2169
593 if (typeset
-p "$_parset_NAME" 2>/dev
/null
; echo) |
594 perl
-ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
595 # This is an associative array
596 # shellcheck disable=SC2006
597 eval "`$_parset_PARALLEL_PRG -k --_parset assoc,"$_parset_NAME" "$@
"`"
598 # The eval returns the function!
600 # This is a normal array or a list of variable names
601 # shellcheck disable=SC2006
602 eval "`$_parset_PARALLEL_PRG -k --_parset var,"$_parset_NAME" "$@
"`"
603 # The eval returns the function!