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?

1 Answer 1


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

  • Works fine here too, but so does removing the \'s and &&'s altogether and just leaving the commands on separate lines... Jan 24, 2011 at 11:02
  • 3
    @elmugrat: But then you lose the conditional chaining. Jan 24, 2011 at 11:05
  • Ahhh true... somehow forgot about that. Jan 24, 2011 at 11:09
  • 3
    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.
    – Jen S.
    Jan 24, 2011 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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