9

I have a very long bash command inside a command substitution like following:

$( comaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaand ) 

I want to know if there is a way to break this command into shorter lines for the sake of readability like following:

$( com aaaaaaaaaaaaaa aaaaaaaaaaaaaa nd ) 

2 Answers 2

16

You can mask the newline with \.

$( com\ aaaaaaaaaaaaaa\ aaaaaaaaaaaaaa\ nd ) 

The \ tells the shell to ignore the newline.

7
  • 2
    The \ part is correct, but you can not indent the subsequent lines. (At least not in the middle of words as in the example.) Commented Jul 8, 2013 at 8:46
  • yeah copy and paste is a curse! :) Commented Jul 8, 2013 at 8:48
  • I checked it and it works even having indentations! Commented Jul 8, 2013 at 8:54
  • 2
    @Coffe_Mug It works only if commaaaaaaaaaaaaaand is actually command with a lot of options and | maybe pipes etc. Otherwise it doesn't work with indentation.Try echo $(ec\<NEWLINEHERE>ho a), works while echo $(ec\<NEWLINE HERE><INDENTATION HERE>ho a) produce ec: command not found. While echo $(echo Hello,\<NEWLINE HERE> World)[note the space after newline] and echo $(echo Hello,<NEWLINE HERE><INDENTATION HERE> World) do exactly the same things. Commented Jul 8, 2013 at 13:28
  • @Bakuriu : to be fair, your last example it does exactly the same thing because you forgot to surround the echo parameters with " (as in : echo "$(echo "Hello,\<NEWLINE HERE> World")" ) so the outer echo receives several parameters and separate them with only 1 default separator. Commented Jul 9, 2013 at 9:14
12

If there are pipe | symbols in a long Bash command line, there may be no need for the backslash \ to mask the newline because the pipe | symbols can be used for formatting your code as well.

# example ls | cat -n | tail | head 
2
  • 1
    oh my, +1, before seing this I always did "commands \<CR> | commands" (which has the advantage of being quite readable, though, as the pipe begin the line, clearly visible. But having to remember to add the "\<CR>" makes it cumbersome) Commented Jul 9, 2013 at 9:11
  • Related: Where are bash line continuations after && and || documented? Commented May 13, 2016 at 20:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.