5

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?

2 Answers 2

7
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.

6
  • 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, 2018 at 11:32
  • @lonix I don't know much about Linux, but both fold and awk are POSIX standard utilities.
    – Kusalananda
    May 26, 2018 at 11:37
  • @lonix To rephrase my previous comment from a few years ago: fold and awk are expected to be available on any Unix system. Some Linux admins may choose to install crippled systems without all basic tools. On those systems, you may not even find a complete set of manuals installed. On the other hand, on non-Linux systems, these tools are standard and always installed. What may be missing on a non-Linux system is the bash shell.
    – Kusalananda
    Feb 7, 2021 at 10:17
  • shell somecommand | fold | sed 's/^/\t/' is alse possible Sep 5, 2021 at 11:05
  • @AntonKorneychuk Yes, if you're using GNU sed. Other sed implementations do not recognize \t as a tab character. In some shells, you could use ... | sed $'s/^/\t/' instead. With standard sed you would need to insert a literal tab character, e.g. by pressing Ctrl+V and then Tab or use ... | sed -e 's/^/@/' -e 'y/@/\t/' assuming @ does not otherwise occur in the data.
    – Kusalananda
    Sep 5, 2021 at 11:14
6

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

3
  • 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, 2018 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, 2018 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 May 26, 2018 at 16:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .