0

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

Andy3153
  • 167
  • 2
    export is a shell builtin that does nothing except put the variable in the environment. it is very, very fast, especially compared to invoking any external command. Just use separate export commands. – Chris Down Mar 11 '24 at 00:27
  • @ChrisDown another question, is alias just as fast? My zshrc contains a similarly formatted alias command for every single alias I have – Andy3153 Mar 11 '24 at 00:48
  • 1
    Yes, alias also is a built-in! What else could it be? It does nothing but change an internal behavior of your shell. As a general role of thumb: do not worry about performance until you have good reason to do so. Second role of thumb: if your shell is becoming too slow, you might want to rewrite your script in something that is not a shell. – sina bala Mar 11 '24 at 00:59
  • 1
    You wrote "which (logically) would be slower (right?)", and that's a really strong indication that you haven't actually witnessed any slowdown. When ever you make a guess that something might be slow, based on how it looks, you need to measure it. Not only to know if there is a difference, but to know if there is a relevant difference. What does your script do? How significant a part of that the export command is? I'd guess that most programs don't call export very often, they likely do it just once for preparation, outside any loops and such. – ilkkachu Mar 11 '24 at 08:19
  • 1
    The parts that usually are slow, are the ones that do a lot of work, move a lot of data, or that get repeated many times. For the rest, it's likely that the time and effort you're spending to wonder about it far outweighs the actual slowness (if there is any). And, well, when you get to the point that very minor things matter, then you should also note that most shells aren't exactly made for being fast, and the main function of shells, running external commands, is also inherently rather slow (in comparison to things that can be done within the same process). – ilkkachu Mar 11 '24 at 08:22
  • 2
    If you're worried about splitting up the export command in particular, you could make the assignments separately, with comments on the in-between lines, and then just run export once for all the variables. (That is, I'm assuming it's the values assigned that warrant comments, not the act of actually exporting each individual variable.) But, anyway, I still suggest measuring if there's any difference at all. – ilkkachu Mar 11 '24 at 08:24
  • @ikkachu I tried to, but time export var1=... does not work – Andy3153 Mar 11 '24 at 08:42
  • 1
    @Andy3153 Running time on a { ..command block.. } does work, though, so you can time one or 1000 exports. – Paul_Pedant Mar 11 '24 at 09:10
  • 2
    @Paul_Pedant, time { export foo=bar; } works in Bash, but there's some quirks with it on zsh. Using a subsell seems to work: time ( for ((i=0; i < 1000000; i++)); do export foo=bar; done ). – ilkkachu Mar 11 '24 at 12:28
  • 2
    @Andy3153, then again, you might want to just time the whole script. That way you'll also see the possible difference in context. Repeating the subject code in a loop over several (thousands of) repetitions is also helpful to make any small differences visible. – ilkkachu Mar 11 '24 at 12:29
  • 2
    "I believe that the answers and comments in here do hold some value too" ... Being closed as a duplicate does not mean that the answers here will be deleted (comments might be, but they get deleted all the time whether the question is closed or not). Voting to leave closed. – muru Mar 12 '24 at 12:41

1 Answers1

3

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