7

I got a shell script and inside I got the command as below

#!/bin/sh
sh ../jboss/bin/standalone.sh --server-config=standalone-full.xml -Djboss.node.name=node1 -b 0.0.0.0 -bmanagement 0.0.0.0

How can I break the line to multiple lines for better understanding and management

#!/bin/sh
sh ../jboss/bin/standalone.sh 
  --server-config=standalone-full.xml
  -Djboss.node.name=node1
  -b 0.0.0.0
  -bmanagement 0.0.0.0

Tried with backslash at the end of each line and it doesnt work.

fpmurphy
  • 4,636

2 Answers2

11

This is known as continuation, and uses \ as the very last character on each line apart from the last one:

#!/bin/sh
sh ../jboss/bin/standalone.sh         \
  --server-config=standalone-full.xml \
  -Djboss.node.name=node1             \
  -b 0.0.0.0                          \
  -bmanagement 0.0.0.0

(aligned for cosmetic purposes, they don’t have to be).

For more on this, see Shell Syntax: How to correctly use \ to break lines? and the linked questions.

Stephen Kitt
  • 434,908
  • \ as the very last character on each line this is very important, avoid space chars after the \ – nulll May 05 '23 at 08:54
7

Working on the idea from Paul_Pedant's comment, in Bash/ksh/zsh you could use an array:

#!/bin/bash
args=(
  --server-config=standalone-full.xml
  -Djboss.node.name=node1
  -b 0.0.0.0
  -bmanagement 0.0.0.0
)
sh ../jboss/bin/standalone.sh "${args[@]}"

Here, the parenthesis provide the syntactic clue the shell needs to know when the array assignment ends. Sadly, this only works with an array assignment, not with just the single command.

Also, if the jboss script is properly set with execute permissions and a hashbang in the script, you should be able to run it as just ../standalone.sh, without the explicit sh in front.

αғsнιη
  • 41,407
ilkkachu
  • 138,973