Robottinosino
Robottinosino

Reputation: 10882

Bash backtick escaping

Is this Bash variable declaration statement:

foo='b'`    `'ar'

less efficient than this:

foo='bar'

Does this way of formatting "hanging" or "aligned" indents (line continuations):

a_long_variable_name='a seemingly infinitely long '`
                    `'string requiring continuatio'`
                    `'n on the next line...'

spawn sub-shells, waste resources, or affect performance in any way, apart from annoying those who dislike its form for (anti?)readability?

Efficiency as in: computer efficiency, not "humans typing many characters and creating space-based, gratuitous maintenance debt".

Can impact (or lack thereof) on performance be easily demonstrated?

Upvotes: 2

Views: 1708

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753990

If I needed it, I'd probably build up the string thus:

x='a seemingly infinitely long '
x="$x"'string requiring continuatio'
x="$x"'n on the next line...'

a_long_variable_name="$x"

Or minor variations on that theme. Or I'd repeat the long variable name on each line. Here's a real script I use — it lists relevant environment variables for me:

informix1="DB[^=]|DELIMIDENT=|SQL|ONCONFIG|TBCONFIG|INFOR"
informix2="CLIENT_LOCALE=|GL_|GLS8BITSYS|CC8BITLEVEL|ESQL|FET_BUF_SIZE="
informix3="INF_ROLE_SEP=|NODEFDAC=|ONCONFIG|OPTCOMPIND|PDQ|PSORT"
informix4="PLCONFIG|SERVER_LOCALE|FGL|C4GL|NE_"
informix5="TCL_LIBRARY|TK_LIBRARY|TERM=|TERMCAP=|TERMINFO="
informix="$informix1|$informix2|$informix3|$informix4|$informix5"
system1="COLLCHAR=|LANG=|LC_"
system2="(DY)?LD_LIBRARY_PATH(_[63][42])?=|PATH=|SHLIB_PATH=|LIBPATH="
system="$system1|$system2"
jlss="IX([A-Z]|D(32|64)?)="

env |
${EGREP:-egrep} "^($informix|$system|$jlss)" |
sort

(And the ability to switch which egrep program to use became necessary when Mac OS X 10.7.x egrep stopped working on the expression because it was 'too big'.)

Upvotes: 0

perreal
perreal

Reputation: 97958

tester1() {
  for i in {1..1000}; do
    a_long_variable_name='a seemingly infinitely long '`
                        `'string requiring continuation'`
                        `'on the next line...'
    echo $a_long_variable_name > tmp 
  done
}

tester2() {
  for i in {1..1000}; do
    a_long_variable_name="a seemingly infinitely long\
string requiring continuation\
on the next line..."
    echo $a_long_variable_name > tmp 
  done
}

echo tester1
time tester1
echo tester2
time tester2

Results

tester1

real    0m1.878s
user    0m0.209s
sys     0m0.566s

tester2

real    0m0.335s
user    0m0.026s
sys     0m0.078s

These all have similar timings to case 2:

read -r -d '' a_long_variable_name <<EOF
    a seemingly infinitely long
    string requiring continuation
    on the next line...
EOF

a_long_variable_name="a seemingly infinitely long\
                      string requiring continuation\
                      on the next line..."

Upvotes: 5

Related Questions