How to export multiple variables with same value in ksh?

Ksh93 (or bash) doesn’t have such expressions, so it’s better
to make it explicit. But you can bundle multiple variables
(with their initial values) in a single export phrase:

export A=1 B=2 C=3

Testing:

$ (export A=1 B=2 C=3 && ksh -c 'echo A=$A B=$B C=$C D=$D')
A=1 B=2 C=3 D=

Awkward alternatives

There is no C-like shortcut, unless you want this ugly thing:

A=${B:=${C:=1}}; echo $A $B $C
1 1 1

… which does not work with export, nor does it work when B or C are empty or non-existent.

Arithmetic notation

Ksh93 arithmetic notation does actually support C-style chained assignments, but for obvious reasons, this only works with numbers, and you’ll then have to do the export separately:

$ ((a=b=c=d=1234))
$ echo $a $b $c $d
1234 1234 1234 1234
$ export a b d
$ ksh -c 'echo a=$a b=$b c=$c d=$d'     # Single quotes prevent immediate substitution
a=1234 b=1234 c= d=d1234                # so new ksh instance has no value for $c

Note how we do not export c, and its value in the child shell is indeed empty.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)