Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 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.

share|improve this answer
Works fine here too, but so does removing the \'s and &&'s altogether and just leaving the commands on separate lines... – Brian Kintz 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. – Brian Kintz 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. – Jen S. Jan 24 '11 at 11:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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