How to output a multiline string in Bash?

后端 未结 9 1627
忘了有多久
忘了有多久 2021-01-29 18:14

How can I output a multipline string in Bash without using multiple echo calls like so:

echo \"usage: up [--level | -n ][--help][--version         


        
相关标签:
9条回答
  • 2021-01-29 18:35

    Here documents are often used for this purpose.

    cat << EOF
    usage: up [--level <n>| -n <levels>][--help][--version]
    
    Report bugs to: 
    up home page:
    EOF
    

    They are supported in all Bourne-derived shells including all versions of Bash.

    0 讨论(0)
  • 2021-01-29 18:42

    One more thing, using printf with predefined var as template.

    msg="First line %s
    Second line %s
    Third line %s
    "
    
    one='additional message for the first line'
    two='2'
    tri='this is the last one'
    
    printf "$msg" "$one" "$two" "$tri"
    

    This ^^^ will print whole message with additional vars inserted instead of '%s' in provided order.

    0 讨论(0)
  • 2021-01-29 18:45

    Inspired by the insightful answers on this page, I created a mixed approach, which I consider the simplest and more flexible one. What do you think?

    First, I define the usage in a variable, which allows me to reuse it in different contexts. The format is very simple, almost WYSIWYG, without the need to add any control characters. This seems reasonably portable to me (I ran it on MacOS and Ubuntu)

    __usage="
    Usage: $(basename $0) [OPTIONS]
    
    Options:
      -l, --level <n>              Something something something level
      -n, --nnnnn <levels>         Something something something n
      -h, --help                   Something something something help
      -v, --version                Something something something version
    "
    

    Then I can simply use it as

    echo "$__usage"
    

    or even better, when parsing parameters, I can just echo it there in a one-liner:

    levelN=${2:?"--level: n is required!""${__usage}"}
    
    0 讨论(0)
  • 2021-01-29 18:49

    Also with indented source code you can use <<- (with a trailing dash) to ignore leading tabs (but not leading spaces).

    For example this:

    if [ some test ]; then
        cat <<- xx
            line1
            line2
    xx
    fi
    

    Outputs indented text without the leading whitespace:

    line1
    line2
    
    0 讨论(0)
  • 2021-01-29 18:51

    Since I recommended printf in a comment, I should probably give some examples of its usage (although for printing a usage message, I'd be more likely to use Dennis' or Chris' answers). printf is a bit more complex to use than echo. Its first argument is a format string, in which escapes (like \n) are always interpreted; it can also contain format directives starting with %, which control where and how any additional arguments are included in it. Here are two different approaches to using it for a usage message:

    First, you could include the entire message in the format string:

    printf "usage: up [--level <n>| -n <levels>][--help][--version]\n\nReport bugs to: \nup home page: \n"
    

    Note that unlike echo, you must include the final newline explicitly. Also, if the message happens to contain any % characters, they would have to be written as %%. If you wanted to include the bugreport and homepage addresses, they can be added quite naturally:

    printf "usage: up [--level <n>| -n <levels>][--help][--version]\n\nReport bugs to: %s\nup home page: %s\n" "$bugreport" "$homepage"
    

    Second, you could just use the format string to make it print each additional argument on a separate line:

    printf "%s\n" "usage: up [--level <n>| -n <levels>][--help][--version]" "" "Report bugs to: " "up home page: "
    

    With this option, adding the bugreport and homepage addresses is fairly obvious:

    printf "%s\n" "usage: up [--level <n>| -n <levels>][--help][--version]" "" "Report bugs to: $bugreport" "up home page: $homepage"
    
    0 讨论(0)
  • 2021-01-29 18:53

    or you can do this:

    echo "usage: up [--level <n>| -n <levels>][--help][--version]
    
    Report bugs to: 
    up home page: "
    
    0 讨论(0)
提交回复
热议问题