程式扎記: [Linux 常見問題] How to output a multiline string in Bash?

標籤

2016年10月14日 星期五

[Linux 常見問題] How to output a multiline string in Bash?

Source From Here
Question
How can I output a multipline string in Bash without using multiple echo calls like so:
  1. echo "usage: up [--level | -n ][--help][--version]"  
  2. echo   
  3. echo "Report bugs to: "  
  4. echo "up home page: "  
I'm looking for a portable way to do this, using only Bash builtins.

How-To
Here documents are often used for this purpose.
  1. cat << EOF  
  2. usage: up [--level | -n ][--help][--version]  
  3.   
  4. Report bugs to:   
  5. up home page:  
  6. EOF  
They are supported in all Bourne-derived shells including all versions of Bash. Another way is using bash built-in command read:
  1. # -d DELIM : The first character of DELIM is used to terminate the input line, rather than newline.  
  2. read -d '' help <<- EOF  
  3.   usage: up [--level | -n ][--help][--version]  
  4.   
  5.   Report bugs to:  
  6.   up home page:  
  7. EOF   
  8.   
  9. echo "$help"  

This message was edited 3 times. Last update was at 15/10/2016 12:23:02

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!