2

I have two variables var1 and var2 that could either be multiple lines or could be completely empty. For example, either one could look like:

line/1
line/2

or


I want to concatenate them so that when put together var1 outputs as is, and var 2 outputs as is right below the last line of var1. There are 4 cases, either var1 is empty and var2 is not, var 2 is empty and var 1 is not, both are empty, or neither are empty. I dont want any whitespace or an empty line if one of the variables is empty. So if var 1 is empty I do not want,


line/1

or vice versa.

Other than using a if, elif, else block, is there a way I could do this or do I HAVE to use an if else block.

In addition, for the last case where neither are empty how can I concatenate these two? I have tried

var3="${var1}\n${var2}" 

but that doesn't seem to work. Any tips would be much appreciated.

1
  • \n does not interpolate a newline character, but a literal ` followed by a n. You can verify this by doing a, say, xxd <<<$var3`. Commented Apr 20, 2020 at 6:20

3 Answers 3

2

Using bash, you can use this function for this job:

concat() {
   printf '%s' "${1:+$1$'\n'}" "${2:+$2$'\n'}";
}

Use of "${1:+$1$'\n'}" appends \n to $1 only if $1 is null/unset.

As per man bash:

${parameter:+word} Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

Examples:

concat 'line1' 'line2'

line1
line2

concat 'line1' ''

line1

concat '' 'line2'

line2

concat '' ''

concat 'line1
line2
line3' ''

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

1 Comment

thank you, this actually worked perfectly. The explanation was very insightful as well!
2
var3="$var1${var1:+${var2:+$'\n'}}$var2"

should work.

$ var1=; var2=; var3="${var1}${var1:+${var2:+$'\n'}}${var2}"; echo "$var3"
               # empty line from echo
$ var1=1; var2=; var3="${var1}${var1:+${var2:+$'\n'}}${var2}"; echo "$var3"
1
$ var1=; var2=2; var3="${var1}${var1:+${var2:+$'\n'}}${var2}"; echo "$var3"
2
$ var1=1; var2=2; var3="${var1}${var1:+${var2:+$'\n'}}${var2}"; echo "$var3"
1
2

  • ${var1} - put var1
  • ${var1:+ - when var1 is set and
    • ${var2:+ - when var2 is set
      • $'\n' - then add a newline
    • }
  • }
  • ${var2} - and add var2 too.

Basically the ${var1:+${var2:+$'\n'}} adds a newline only if var1 and var2 are both set, to separate var1 and var2.

For reference bash manual shell parameter expansion.

1 Comment

It's literally explained below. Maybe refresh the page, I edited the post.
1

You can try using printf -v

printf -v var3 '%s\n "$var1" "$var2"

4 Comments

what exactly does the -v modifier do? What does the %s\n do?
Still, when var1 or var2 are empty, this will result in empty lines.
@KamilCuk indeed
@nutella100 help prinf -v var assign the output to shell variable VAR rather than display it on the standard output

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.