0

I have a BASH script with a condition similar to this one:

dirImg="/var/log/junk"
sizeLimitMsg="1 gig"

if [ 0 == "$fileSize1" ];then
    echo "All directories under $dirImg are within the allowed\
    $sizeLimitMsg size limit" >> $emailBody
else
    #do something else
fi

It produces output with extra white space:

All directories under /var/log/junk are within the allowed   1 gig size limit

I'm guessing the extra and unwanted white space is coming from indenting the variable $sizeLimitMsg in the conditional above.

Is there anyway I can both continue that line and indent that code without getting the extra and unwanted white space?

Steve
  • 3,127
  • 14
  • 56
  • 96

2 Answers2

3

Don't include the indentation into the quotes:

echo "All directories under $dirImg are within the allowed" \
     "$sizeLimitMsg size limit"
choroba
  • 231,213
  • 25
  • 204
  • 289
1

Alternative:

printf "%s %s\n" "All directories under ${dirImg} are within the allowed" \
    "${sizeLimitMsg} size limit" >> "${emailBody}"
Walter A
  • 19,067
  • 2
  • 23
  • 43
  • 1
    Or perhaps `printf 'All directories under %s are within the %i size limit\n' "$dirImg" "$sizeLimitMsg"` perhaps with an escaped newline after the format string if you like. – tripleee Nov 28 '18 at 04:21
  • @tripleee I agree that is a nicer format. Trying to defend my worse format: I am afraid the OP will try to add random text in the format string and will change the message into `printf "All directories under %s are safe under the %s % limit\n" "${dirImg}" "${percUsageAllowed}"` without escaping `%`. – Walter A Nov 28 '18 at 07:48