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

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...