How do you wrap a long command to the next line within a bash script file?

As a simple example, I want to run the command "pushd . && cd /foo/bar && ls && popd"

From the console I can do this:

pushd . \
&& cd /foo/bar \
&& ls \
&& popd

And that wraps the line.

But the same code in a script file produces an error.

How do you wrap these lines to be nicely formatted?

link|improve this question
feedback

1 Answer

up vote 2 down vote accepted

Works fine here. Make sure that the backslash is the very last character on the line, and that the file uses *nix line endings.

link|improve this answer
Works fine here too, but so does removing the \'s and &&'s altogether and just leaving the commands on separate lines... – elmugrat Jan 24 '11 at 11:02
@elmugrat: But then you lose the conditional chaining. – Ignacio Vazquez-Abrams Jan 24 '11 at 11:05
Ahhh true... somehow forgot about that. – elmugrat Jan 24 '11 at 11:09
this problem was the line endings, which is due to the fact that I'm using cygwin on windows and not a different OS. Thanks. – Hamlet D'Arcy Jan 24 '11 at 11:58
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.