0

Which form is most efficient?

1)

v=''
v+='a'
v+='b'
v+='c'

2)

v2='a'` `'b'` `'c'

Assuming readability were exactly the same to you, and that's a stretch, would 1) mean creating and throwing away a few string immutables (like in Python) or act as a Java "StringBuffer" with periodical expansion of the buffer capacity? How are string concatenations handled internally in Bash?

If 2) were just as readable to you as 1), would the backticks spawn subshells and would that be more costly, even as a potential 'no-op' than what is done in 1) ?

2 Answers 2

2

Well, the simplest and most efficient mechanism would be option 0:

v="abc"

The first mechanism involves four assignments.

The second mechanism is bizarre (and is definitely not readable). It (nominally) runs an empty command in two sub-shells (the two ` ` parts) and concatenates the outputs (an empty string) with the three constants. If the shell simply executes the back-tick commands without noting that they're empty (and it's not unreasonable that it won't notice; it is a weird thing to try — I don't recall seeing it done in my previous 30 years of shell scripting), this is definitely vastly slower.

So, given only options (1) and (2), use option (1), but in general, use option (0) shown above.

Why would you be building up the string piecemeal like that? What's missing from your example that makes the original code sensible but the reduced code shown less sensible.

v=""
x=$(...)
v="$v$x"
y=$(...)
v="$v$y"
z=$(...)
v="$v$z"

This would make more sense, especially if you use each of $x, $y and $z later, and/or use intermediate values of $v (perhaps in the commands represented by triple dots). The concatenation notation used will work with any Bourne-shell derivative; the alternative += shell will work with fewer shells, but is probably slightly more efficient (with the emphasis on 'slightly').

Sign up to request clarification or add additional context in comments.

5 Comments

It is very odd that the parser should not notice the lack of tokens within the backticks, collapsing to no-op. Anyway, you are right.. the piecemeal variables are used elsewhere.. I compressed the example.
30 years of scripting.. whoa, BTW. I bow in respect!! Thanks for your answer! Do you happen to know how string concatenation works internally? I suppose one could look at the source..
I've never seen anyone who wanted to run nothing in a sub-shell and capture the output. Is it inspired by knowledge of Python, perhaps? I've not looked, but I'd be astonished if the shell was not using mutable strings for each variable's value. Immutables as in Python is a much more recent idea.
@JonathanLeffler: see stackoverflow.com/questions/15537800/bash-backtick-escaping. He's looking for ways to add ignored whitespace to his source code.
I now use a mixture of += and ...\n.... for spacing out and visually aligning code.. awarding your answer as it's the only one applicable. Thanks!
0

The portable and straight forward method would be to use double quotes and curly brackets for variables:

VARA="beginning text ${VARB} middle text ${VARC}..."

you can even set default values for empty variables this way

VARA="${VARB:-default text} substring manipulation 1st 3 characters ${VARC:0:3}"

using the curly brackets prevents situations where there is a $VARa and you want to write ${VAR}a but end up getting the contents of ${VARa}

3 Comments

Your comments are accurate, but don't really address the question.
@JonathanLeffler - yeah, you already addressed that part, I was addressing the other side of efficiency - writing it so it doesn't break when you add new variables... and double quotes are much more efficient (and readable) than doing 'single quotes around text and smashing '$VARS'in like this'
OK; maybe you should have explained what you were addressing. I'd like to see evidence that double quotes are 'much more efficient' than single quotes. Single quotes are actually easier for the shell to process, but the difference is negligible so for most practical purposes, they'll be the same. I agree that double quotes are 'more readable' (and so more efficient in the use of programmer's time), especially if you need to interpolate variables into the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.