Is a comment in the middle of a command possible?

I want to export multiple variables and to do that I decided to put them in a single export command so that I don’t call export multiple times for no reason, which (logically) would be slower (right?)

Like this:

export
       var1=a
       var2=b
       var3=c
       
       [ ... ]

       varn=zzz

But this doesn’t allow me to add comments in between anymore:

export
       # Comment
       var1=a
       var2=b # Comment 
       var3=c
       
       [ ... ]

       varn=zzz

since backslashes put things all on the same line.

How could I go about commenting the variables I want to export? I was thinking I could just call export for each variable, but I don’t want to slow down my script with this just for the sake of commenting.

EDIT for duplicate message: while the other questions have answers that solve my issue too, I believe that the answers and comments in here do hold some value too

Asked By: Andy3153

||

Yes, kind of. You can put a command substitution pretty much anywhere, and you can put a comment in a command substitution. It’s ugly, but it works.

export
       $(# Comment
       ) 
       var1=a
       var2=b $(# Comment 
       ) 
       var3=c

However, it’s something you’d do if you really can’t do it another way. In almost every case where you could do this, it’s more readable to split the command into multiple parts, with comments attached to each part if necessary. Some comments might not even be necessary if you pick sensible variable names.

It’s definitely not something you’d do for performance. Calling an external command is somewhat expensive, but calling a built-in command isn’t. It’s unlikely that splitting an export command makes an observable difference to the time it takes to run your .zshrc. (I benchmarked below, and it actually does make a difference if you benchmark that one line, but on the scale of a typical .zshrc, this is completely negligible.) In any case, calling a built-in command is cheaper than a command substitution, so the $(…) thing above will make your script slower.


Is calling export for each variable slower than calling it once per variable? Yes, a little bit. At least for the particular code, version and platform I tested: the difference is small enough that it could vary.

% time zsh -c 'for ((i=0; i<$1; i++)) { export var1=foo var2=bar; }' 1 1000000
zsh -c 'for ((i=0; i<$1; i++)) { export var1=foo var2=bar; }' 1 1000000  1.36s user 0.24s system 99% cpu 1.612 total
% time zsh -c 'for ((i=0; i<$1; i++)) { export var1=foo; export var2=bar; }' 2 1000000                            
zsh -c 'for ((i=0; i<$1; i++)) { export var1=foo; export var2=bar; }' 2   1.67s user 0.43s system 99% cpu 2.097 total

However, adding a command substitution makes it 100 times slower! (Note the reduced number of iterations.)

% time zsh -c 'for ((i=0; i<$1; i++)) { export var1=foo var2=bar $(); }' 3 10000
zsh -c 'for ((i=0; i<$1; i++)) { export var1=foo var2=bar $(); }' 3 10000  1.33s user 1.00s system 107% cpu 2.165 total
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.