How Linux uses the Echo directive to write content to a file

Source: Internet
Author: User
Tags echo command string back

0. Preface This article summarizes how to use the echo command to write to a file, such as using the ECHO directive to overwrite the contents of a file, using the Echo directive to append content to a file, and using the Echo Directive to append tabs to the file. The basic way for Echo to output content to a file is to use the IO redirect Directive--">", which, by default, is output to the standard output, using the > directive to redirect output to a file. 1.echo instruction Basic Usage "1" linux official User manual--echo instruction "2" input instruction get help sudo echo--help return content as follows [Plain]View PlainCopy
  1. Usage: echo [short options] ... [String] ...
  2. Or: Echo Long option
  3. Echoes a STRING back to standard output.
  4. -n do not trailing line breaks
  5. -e enable escape function to interpret backslashes
  6. -e disables the escape function that interprets backslashes (default)
  7. --HELP Display this help message and exit
  8. --version display version information and exit
  9. If-e is available, the following sequence is recognized:
  10. \ \ counter Slash
  11. \a Ring Ring
  12. \b Backspace
  13. \c no longer produces new output
  14. \e Escape character
  15. \f Page Change
  16. \ n New Line
  17. \ r Enter
  18. \ t Horizontal tab
  19. \v Vertical Tab
  20. \0nnn bytes are expressed in octal NNN (1 to 3 bits)
  21. \xhh bytes in hexadecimal number HH (1 to 2 bits)
2. Overwrite the file contents "Sample Script" test.sh use the > directive to overwrite the original contents of the file and re-enter the content, if the file does not exist, create the file.    #!/bin/bashecho "Raspberry" > test.txt "Operation Process" # Modify permissions, script executable chmod u+x test.sh./test.sh "File Contents" Raspberry 3. Append file contents The sample script test.sh uses the >> directive to append content to the file, and the original content is saved. [Plain]View PlainCopy
    1. #!/bin/bash
    2. echo "Raspberry" > Test.txt
    3. echo "Intel Galileo" >> test.txt
"Procedure" # Modify permissions, script executable chmod u+x test.sh./test.sh "File contents" Note that the Echo directive adds a carriage return (\ n) By default at the end of the line, so there are two lines displayed here. Raspberryintel Galileo 4. Enter the transfer character using the-e parameter to enable the transfer character. The following is a JSON packet written to the file via the echo command. If you are unfamiliar with JSON format, please refer to--"front End Learning--json format" "Sample Script" test-json.sh [HTML]View PlainCopy
    1. #!/bin/bash
    2. Echo-e "{" > Test-json.txt
    3. Echo-e "\t\" name\ ": \" xukai871105\ "" >> Test-json.txt
    4. Echo-e "}" >> Test-json.txt
"description" Here two transfer characters are used, \ t tab, \ "double quotation mark. "Operation Procedure" # Modify permissions, script executable chmod u+x test-json.sh./test-json.sh "File contents" {"Name": "xukai871105"}5. Use the file name in 3 of the script above the variable    Test-json.txt, if the file name needs to be modified then there is a need to modify 3, so the operation seems troublesome, in order to simplify the operation can use variables to simplify the script. "Sample Script" test-json.sh [Plain]View PlainCopy
    1. #!/bin/bash
    2. File= "Test-json.txt"
    3. Echo-e "{" > $FILE
    4. Echo-e "\t\" name\ ": \" xukai871105\ "" >> $FILE
    5. Echo-e "}" >> $FILE
"Operation Procedure" # Modify permissions, script executable chmod u+x test-json.sh./test-json.sh "File contents" {"Name": "Xukai871105"} 6. There are many techniques to summarize Linux, which need to be slow Slow accumulation. Add an oil to yourself.

How Linux uses the Echo directive to write content to a file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.