Search code examples
bashdry

How to print a large block of text in bash without numerous echo lines?


Let's say I want to show the usage of my fancy bash script with the function:

function show_usage()
{
   echo "Auto generates a recipe for the day!"
   echo "my_script [-a][-b][-c]"
   echo 
   echo "To create a recipe use the three options to alter the type of recipe."
   echo "Options:"
   echo " -a: Add any extras/toppings"
   echo " -b: Allow experimental recipes (beta)"
   echo " -c: Cut out any waste."
   echo
   echo "Lorem ipsum natus sit voluptatem accusantium..."
}

You get the idea... All those echos start to get a bit tiresome and look messy.

Is there an approach for this in bash that doesn't use a bunch of echo lines?


Solution

  • You could use bash heredoc.

    For example:

    cat << \
    =========================================================================
    Auto generates a recipe for the day!
    my_script [-a][-b][-c]
    
    To create a recipe use the three options to alter the type of recipe.
    Options:
     -a: Add any extras/toppings
     -b: Allow experimental recipes (beta)
     -c: Cut out any waste.
    
    Lorem ipsum natus sit voluptatem accusantium...
    =========================================================================
    

    The first & last lines act as beginning and end, they can be any string you like, but they have to be identical.