skip to content
Alvin Lucillo

Multiline commands in Bash Part 1

/ 1 min read

💻 Tech

Let’s say you want have a long command of az cli command. The command below can be longer with several arguments not included in the example, but let’s just stick with this for now. az storage blob list --account-name hello --output table --debug

Now, for some reason, if you want to break them apart, you can do:

  1. String concatenation
  2. Multiline string value
  3. Heredoc (discussed in the next tech journal)

String concatenation - bit redundant; there’s better options

#!/bin/bash
debug=true

cmd="az storage blob list"
cmd+=" --acount-name hello"                  
cmd+=" --output table"
cmd+=" ${debug:+--debug}"

echo $cmd
# output:
# az storage blob list --acount-name hello --output table --debug

Multiline string value — better than string concatenation above; here, the parts of the string are broken and \ signifies the string value is continued at the next line.

#!/bin/bash
debug=true
cmd="az storage blob list \
        --account-name hello \
        --output table
        ${debug:+--debug}"

echo $cmd
# output:
# az storage blob list --account-name hello --output table --debug