Released as 20250122 ('4K-AZ65')
[parallel.git] / src / env_parallel.mksh
blob5e31033be6dddefce4b7a254a9fde9d20439214a
1 #!/usr/bin/env mksh
3 # This file must be sourced in sh/ash/dash/bash/ksh/mksh/zsh:
5 #   . env_parallel.sh
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
38 env_parallel() {
39     # env_parallel.{sh,ash,dash,bash,ksh,mksh,zsh}
41     # Check shell dialect
42     if [ -n "$BASH_VERSION" ]; then
43         _shell_DIALECT=bash
44         _eval_needed=false
45         _prefix_PARALLEL_ENV=_prefix_PARALLEL_ENV_bash
46     elif [ -n "$ZSH_VERSION" ]; then
47         _shell_DIALECT=zsh
48         _eval_needed=true
49         _prefix_PARALLEL_ENV=false
50     elif [ -n "$KSH_VERSION" ]; then
51         _shell_DIALECT=ksh
52         _eval_needed=false
53         _prefix_PARALLEL_ENV=false
54     else
55         # Dash/ash - can these be detected better?
56         _shell_DIALECT="sh"
57         _eval_needed=false
58         _prefix_PARALLEL_ENV=false
59     fi
60     _names_of_ALIASES() {
61         _names_of_ALIASES_$_shell_DIALECT
62     }
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
69                 echo "$_i"
70             fi
71         done
72     }
73     _names_of_ALIASES_bash() {
74         # No aliases will return false. This error should be ignored.
75         # shellcheck disable=SC3044
76         compgen -a || true
77     }
78     _names_of_ALIASES_ksh() {
79         alias | perl -pe 's/=.*//'
80     }
81     _names_of_ALIASES_zsh() {
82         # shellcheck disable=SC2086,SC2296
83         print -l ${(k)aliases}
84     }
85     _bodies_of_ALIASES() {
86         _bodies_of_ALIASES_$_shell_DIALECT "$@"
87     }
88     _bodies_of_ALIASES_sh() {
89         # alias may return:
90         #   myalias='definition' (GNU/Linux ash)
91         #   alias myalias='definition' (FreeBSD ash)
92         # so remove 'alias ' from first line
93         for _i in "$@"; do
94                 echo 'alias '"`alias "$_i" | perl -pe '1..1 and s/^alias //'`"
95         done
96     }
97     _bodies_of_ALIASES_bash() {
98         # shellcheck disable=SC3043
99         local _i
100         for _i in "$@"; do
101             # shellcheck disable=SC2046
102             if [ $(alias "$_i" | wc -l) = 1 ] ; then
103                 true Alias is a single line. Good.
104             else
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'."
108             fi
109         done
110         alias "$@"
111     }
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\".");
119                      }'
121     }
122     _bodies_of_ALIASES_zsh() {
123         # shellcheck disable=SC3043
124         local _i
125         for _i in "$@"; do
126                 echo 'alias '"$(alias "$_i")"
127         done
128     }
129     _names_of_FUNCTIONS() {
130         _names_of_FUNCTIONS_$_shell_DIALECT
131     }
132     _names_of_FUNCTIONS_bash() {
133         # shellcheck disable=SC3044
134         compgen -A function
135     }
136     _names_of_maybe_FUNCTIONS() {
137         set | perl -ne '/^([A-Z_0-9]+)\s*\(\)\s*\{?$/i and print "$1\n"'
138     }
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"'
144     }
145     _names_of_FUNCTIONS_ksh() {
146         # shellcheck disable=SC3044
147         typeset +f | perl -pe 's/\(\).*//; s/ .*//;'
148     }
149     _names_of_FUNCTIONS_zsh() {
150         # shellcheck disable=SC2086,SC2296
151         print -l ${(k)functions}
152     }
153     _bodies_of_FUNCTIONS() {
154         _bodies_of_FUNCTIONS_$_shell_DIALECT "$@"
155     }
156     _bodies_of_FUNCTIONS_sh() {
157         LANG=C type "$@" | perl -ne '/^(\S+) is a function$/ or print'
158     }
159     _bodies_of_FUNCTIONS_bash() {
160         # shellcheck disable=SC3044
161         typeset -f "$@"
162     }
163     _bodies_of_FUNCTIONS_ksh() {
164         functions "$@"
165     }
166     _bodies_of_FUNCTIONS_zsh() {
167         # shellcheck disable=SC3044
168         typeset -f "$@"
169     }
170     _names_of_VARIABLES() {
171         _names_of_VARIABLES_$_shell_DIALECT
172     }
173     _names_of_VARIABLES_sh() {
174         # This may screw up if variables contain \n and =
175         set | perl -ne 's/^(\S+?)=.*/$1/ and print;'
176     }
177     _names_of_VARIABLES_bash() {
178         # shellcheck disable=SC3044
179         compgen -A variable
180     }
181     _names_of_VARIABLES_ksh() {
182         # mksh: typeset +p |
183         #    perl -pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
184         #    uniq
185         # ksh: typeset +p | perl -pe 's/^typeset .. //'
186         # shellcheck disable=SC3044
187         typeset +p |
188             perl -pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
189             uniq
190     }
191     _names_of_VARIABLES_zsh() {
192         # shellcheck disable=SC2086,SC2296
193         print -l ${(k)parameters}
194     }
195     _bodies_of_VARIABLES() {
196         _bodies_of_VARIABLES_$_shell_DIALECT "$@"
197     }
198     _bodies_of_VARIABLES_sh() {
199         # Crappy typeset -p
200         for _i in "$@"
201         do
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;";
205             echo
206         done
207     }
208     _bodies_of_VARIABLES_bash() {
209         # shellcheck disable=SC3044
210         typeset -p "$@"
211     }
212     _bodies_of_VARIABLES_ksh() {
213         # shellcheck disable=SC3044
214         typeset -p "$@"
215     }
216     _bodies_of_VARIABLES_zsh() {
217         # shellcheck disable=SC3044
218         typeset -p "$@"
219     }
220     _ignore_HARDCODED() {
221         _ignore_HARDCODED_$_shell_DIALECT
222     }
223     _ignore_HARDCODED_sh() {
224         # These names cannot be detected
225         echo '(_|TIMEOUT|IFS)'
226     }
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_]+)'
231     }
232     _ignore_HARDCODED_ksh() {
233         # These names cannot be detected
234         echo '(_|TIMEOUT|IFS)'
235     }
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)'
239     }
240     _ignore_READONLY() {
241         _ignore_READONLY_$_shell_DIALECT
242     }
243     _parse_READONLY() {
244         # shellcheck disable=SC1078,SC1079,SC2026
245         perl -e '@r = map {
246                 chomp;
247                 # sh on UnixWare: readonly TIMEOUT
248                 # ash: readonly var='val'
249                 # ksh: 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/;
255                 $_ } <>;
256             $vars = join "|",map { quotemeta $_ } @r;
257             print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
258             '
259     }
260     _ignore_READONLY_sh() {
261         readonly | _parse_READONLY
262     }
263     _ignore_READONLY_bash() {
264         readonly | _parse_READONLY
265     }
266     _ignore_READONLY_ksh() {
267         readonly | _parse_READONLY
268     }
269     _ignore_READONLY_zsh() {
270         # shellcheck disable=SC3044
271         typeset -pr | _parse_READONLY
272     }
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 '^(...)$' |
281         perl -ne '/^(
282                      PARALLEL_ENV|
283                      PARALLEL_TMP|
284                      _alias_NAMES|
285                      _bodies_of_ALIASES|
286                      _bodies_of_FUNCTIONS|
287                      _bodies_of_VARIABLES|
288                      _error_PAR|
289                      _function_NAMES|
290                      _get_ignored_VARS|
291                      _grep_REGEXP|
292                      _ignore_HARD|
293                      _ignore_HARDCODED|
294                      _ignore_READONLY|
295                      _ignore_RO|
296                      _ignore_UNDERSCORE|
297                      _list_alias_BODIES|
298                      _list_function_BODIES|
299                      _list_variable_VALUES|
300                      _make_grep_REGEXP|
301                      _names_of_ALIASES|
302                      _names_of_FUNCTIONS|
303                      _names_of_VARIABLES|
304                      _names_of_maybe_FUNCTIONS|
305                      _parallel_exit_CODE|
306                      _prefix_PARALLEL_ENV|
307                      _prefix_PARALLEL_ENV_bash|
308                      _remove_bad_NAMES|
309                      _remove_readonly|
310                      _variable_NAMES|
311                      _warning_PAR|
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;
319             print;'
320     }
321     _prefix_PARALLEL_ENV_bash() {
322         # shellcheck disable=SC3044
323         shopt 2>/dev/null |
324         perl -pe 's:\s+off:;: and s/^/shopt -u /;
325                   s:\s+on:;: and s/^/shopt -s /;
326                   s:;$:&>/dev/null;:';
327         echo 'shopt -s expand_aliases &>/dev/null';
328     }
330     _get_ignored_VARS() {
331         perl -e '
332             for(@ARGV){
333                 $next_is_env and push @envvar, split/,/, $_;
334                 $next_is_env=/^--env$/;
335             }
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";
340                 } else {
341                     chomp(@ignored_vars = <IN>);
342                 }
343             }
344             if($ENV{PARALLEL_IGNORED_NAMES}) {
345                 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
346                 chomp @ignored_vars;
347             }
348             $vars = join "|",map { quotemeta $_ } @ignored_vars;
349             print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
350             ' -- "$@"
351     }
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() {
358         perl -e '
359             for(@ARGV){
360                 /^_$/ and $next_is_env = 0;
361                 $next_is_env and push @envvar, split/,/, $_;
362                 $next_is_env = /^--env$/;
363             }
364             $vars = join "|",map { quotemeta $_ } @envvar;
365             print $vars ? "($vars)" : "(.*)";
366             ' -- "$@"
367     }
368     _which_PAR() {
369         # type returns:
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
380         LANG=C type "$@" |
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 }'
389     }
390     _warning_PAR() {
391         echo "env_parallel: Warning: $*" >&2
392     }
393     _error_PAR() {
394         echo "env_parallel: Error: $*" >&2
395     }
397     if _which_PAR parallel >/dev/null; then
398         true parallel found in path
399     else
400         # shellcheck disable=SC2016
401         _error_PAR 'parallel must be in $PATH.'
402         return 255
403     fi
405     # Grep regexp for vars given by --env
406     # shellcheck disable=SC2006
407     _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
408     unset _make_grep_REGEXP
410     # Deal with --env _
411     # shellcheck disable=SC2006
412     _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
413     unset _get_ignored_VARS
415     # --record-env
416     if perl -e 'exit grep { /^--record-?env$/ } @ARGV' -- "$@"; then
417         true skip
418     else
419         (_names_of_ALIASES;
420          _names_of_FUNCTIONS;
421          _names_of_VARIABLES) |
422             cat > "$HOME"/.parallel/ignored_vars
423         return 0
424     fi
426     # --session
427     if perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
428         true skip
429     else
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\";
434           echo :::;
435           (_names_of_ALIASES;
436            _names_of_FUNCTIONS;
437            _names_of_VARIABLES) | perl -ne '
438             BEGIN{
439               map { $ignored_vars{$_}++ }
440                 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
441             }
442             chomp;
443             for(split/\s+/) {
444               if(not $ignored_vars{$_}) {
445                 print $_,\"\\n\";
446               }
447             }
448             '`"
449         export PARALLEL_IGNORED_NAMES
450         return 0
451     fi
452     if perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
453         true skip
454     else
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}
460         '`"
461         return 0
462     fi
463     # Grep alias 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"
470     fi
471     unset _alias_NAMES
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"
480     fi
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"
490     fi
491     unset _variable_NAMES
493     if $_eval_needed ; then
494         # shellcheck disable=SC2006
495         PARALLEL_ENV="`
496         eval $_prefix_PARALLEL_ENV;
497         eval $_list_alias_BODIES;
498         eval $_list_function_BODIES;
499         eval $_list_variable_VALUES;
500         `"
501     else
502         # shellcheck disable=SC2006
503         PARALLEL_ENV="`
504         $_prefix_PARALLEL_ENV;
505         $_list_alias_BODIES;
506         $_list_function_BODIES;
507         $_list_variable_VALUES;
508         `"
509     fi
510     export PARALLEL_ENV
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
540         parallel "$@"
541         _parallel_exit_CODE=$?
542         # Clean up variables/functions
543         unset PARALLEL_ENV
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"
548     else
549         unset PARALLEL_ENV;
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"
559         return 255
560     fi
563 parset() {
564     _parset_PARALLEL_PRG=parallel
565     _parset_main "$@"
568 env_parset() {
569     _parset_PARALLEL_PRG=env_parallel
570     _parset_main "$@"
573 _parset_main() {
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
578     #
579     #   # Create array named myvar
580     #   parset myvar echo ::: {1..10}
581     #   echo ${myvar[5]}
582     #
583     #   # Put output into $var_a $var_b $var_c
584     #   varnames=(var_a var_b var_c)
585     #   parset "${varnames[*]}" echo ::: {1..3}
586     #   echo $var_c
587     #
588     #   # Put output into $var_a4 $var_b4 $var_c4
589     #   parset "var_a4 var_b4 var_c4" echo ::: {1..3}
590     #   echo $var_c4
592     _parset_NAME="$1"
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
597         return 255
598     fi
599     if [ "$_parset_NAME" = "--help" ] ; then
600         echo parset: Error: Usage: >&2
601         echo parset: Error: ' ' parset varname GNU Parallel options and command >&2
602         echo
603         parallel --help
604         return 255
605     fi
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."
614         echo
615         echo "Web site: https://www.gnu.org/software/parallel"
616         echo
617         echo "When using programs that use GNU Parallel to process data for publication"
618         echo "please cite as described in 'parallel --citation'."
619         echo
620         return 255
621     fi
622     shift
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!
634     else
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!
639     fi