3

I have a bash script that echoes paragraphs of text. I want them to be indented.

Example:

echo "Something"
echo -e "\tfoo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo"

Will print something like this:

Something
      foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo 
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo 
foo foo foo foo foo foo foo foo foo foo foo foo

But what I want is this:

Something
    foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo 
    foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo 
    foo foo foo foo foo foo foo foo foo foo foo foo

I prefer to avoid having to line-break many such paragraphs by hand.

I've seen all sorts of techniques using sed and so on, but I need to rely on shell builtins only (should be as simple as possible).

UPDATE
Put it this way- how do the man pages (for any random command) format all those paragraphs so nicely? Surely they were not line-breaked by hand? And I assume they didn't use anything other than builtins when documenting the basic commands?

4

How about fmt (from GNU Coreutils)

{ echo "Something"
echo -e "\tfoo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo"
} | fmt
Something
    foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
    foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
    foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
    foo foo foo foo foo foo foo foo foo foo foo foo

You can add an optional -w n to change the length of the wrapped lines to n

  • Thanks. Is fmt an "addon", or can I rely on it being available in most linuxes? (I use Ubuntu and I see that I have it though). Is "GNU CoreUtils" part of the "core" of the OS, if that is the right terminology? – lonix May 26 '18 at 15:45
  • Also do you know why if I put it on one line { echo -e "foo" } | fmt it says "syntax error: unexpected end of file" but your way works? – lonix May 26 '18 at 16:07
  • I'm not an expert but I would expect that CoreUtils would be part of any GNU/Linux system since it provides basic command such as ls, rm etc. To make the command group work in a one-liner you need to add a semicolon: { echo -e "foo"; } | fmt – steeldriver May 26 '18 at 16:14
1
somecommand | fold | awk '{ print "\t" $0 }'

This would take the output from somecommand (for example echo), send it through fold, which would fold it at 80 characters (use fold -w N to fold at N characters). Then the awk script will print each line indented by a tab character.

Doing folding in pure bash is bound to result in a more complicated script.

  • Thanks. Is awk an "addon", or can I rely on it being available in most linuxes? (I use Ubuntu and I see that I have it though) – lonix May 26 '18 at 11:32
  • @lonix I don't know much about Linux, but both fold and awk are POSIX standard utilities. – Kusalananda May 26 '18 at 11:37

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.