Linux Learning notes-How to write content to a file using the Echo Directive __linux

Source: Internet
Author: User
Tags chmod echo command string back
0. Preface This article summarizes how to write content to a file using the echo command, such as overwriting the contents of the file with the Echo directive, appending content to the file with the Echo directive, and appending tabs to the file with the Echo directive. The basic method of ECHO's output to a file is to use the IO redirection instruction-">", by default echo output to 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
Usage: echo [short option] ... [String] ...
Or: The Echo long option

echoes STRING back to standard output.

  -N non-trailing newline character
  -e enable interpretation of the escape feature of backslashes-
  e disable the escape feature for backslash (default)
      --HELP displays this help information and exits
      --version display version information and exits
if-e is available, The following sequence can be identified:
  \    backslash
  \a    ring
  \b    backspace
  \c    no longer produces a new output
  \e    escape character  
  \f    change page
  \ n    New line
  \ r    /t horizontal tab
  \v    vertical tab
  \0nnn   bytes to octal number NNN ( 1 to 3 bits indicates    the number of \xhh bytes in the hexadecimal number of HH (1 to 2 digits)

2. Overwrite the contents of the File "sample script" test.sh use > instructions to overwrite the original contents of the file and re-enter the content, if the file does not exist, create the file. #!/bin/bash echo "Raspberry" > test.txt "Operation Process" # Modify permissions, script executable chmod u+x test.sh./test.sh "File contents" Raspberry
3. Append file Content "Sample script" test.sh use >> instructions to append content to the file, the original content will be saved.
#!/bin/bash
echo "Raspberry" > Test.txt
echo "Intel Galileo" >> test.txt
"Operation Procedure" # Modify permissions, script executable chmod u+x test.sh./test.sh "File content" note that the Echo Directive increases carriage return (\ n) By default at the end of the line, so two lines appear here. Raspberry Intel Galileo
4. Input transfer characters Use the-e parameter to enable the transfer character. The following echo command is used to write a JSON packet to the file. If you are unfamiliar with the JSON format please refer to the "Front-End Learning--json format" "Sample Script" test-json.sh
#!/bin/bash
echo-e "{" > Test-json.txt
echo-e "\t\" name\ ": \" xukai871105\ "" >> test-json.txt
Echo-e "}" >> Test-json.txt
"description" Here are two transfer characters, \ t tabs, \ double quotes. "Operation Procedure" # Modify permissions, script executable chmod u+x test-json.sh./test-json.sh "File content" {"name": "Xukai871105"} 5. Using variables in the script above     3 uses the file name Test-json.txt, if the file name needs to modify then needs to modify 3, such operation is somewhat troublesome, in order to simplify the operation can use the variable simplification script. "Sample Script" test-json.sh
#!/bin/bash
file= "test-json.txt"
echo-e "{" > $FILE echo-e "\t\" name\ "
: \" Xukai871105\ "" >> $ FILE
echo-e "}" >> $FILE
"Operation Procedure" # Modify permissions, script executable chmod u+x test-json.sh./test-json.sh "File content" {"name": "Xukai871105"}
6. There are still a lot of skills to summarize Linux, it needs to accumulate slowly. Add an oil to yourself.



Related Article

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.