Linux Learning notes-how to write content to a file using the echo command

Source: Internet
Author: User
Tags echo command string back

0. PrefaceThis article summarizes how to use the echo command to write to a file, such as using the ECHO directive to overwrite the contents of the file, appending the content to the file using the Echo Directive, and appending tabs to the file using the echo instruction. 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
Usage: echo [short options] ... [String] ... Or: The Echo long option echoes the STRING back to standard output. -  n does not follow newline characters  -e enables the escape function of the interpreted backslash-  e disables the escape function that interprets backslashes (default)      --HELP displays this help information and exits      --version displays version information and exits if-e is available, The following sequence is recognized:  \ \    backslash  \a    ring ringtone  \b    backspace  \c    no longer produces new output  \e    escape character    \f    page break  \ n    New line  \ r    return  \ t    horizontal tab  \v    vertical tab  \0nnn   bytes in octal NNN (1 to 3-bit)  \xhh    Bytes in hexadecimal number HH (1 to 2 bits)

2. Overwrite file contentsThe sample script test.sh uses the > directive to overwrite the original contents of the file and re-enter the content, creating the file if the file does not exist. #!/bin/bashecho "Raspberry" > test.txt "Operation Process" # Modify permissions, script executable chmod u+x test.sh./test.sh "File Contents" Raspberry
3. append file ContentsThe sample script test.sh uses the >> directive to append content to the file, and the original content is saved.
#!/bin/bashecho "Raspberry" > Test.txtecho "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 characterUse 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
#!/bin/bashecho-e "{" > Test-json.txtecho-e "\t\" name\ ": \" xukai871105\ "" >> Test-json.txtecho-e "}" >> t Est-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. Using Variables3 of the above script uses the file name Test-json.txt, if the file name needs to be modified then it needs to be modified 3, this operation seems troublesome, in order to simplify the operation can use variables to simplify the script. "Sample Script" test-json.sh
#!/bin/bashfile= "Test-json.txt" echo-e "{" > $FILEecho-E "\t\" name\ ": \" xukai871105\ "" >> $FILEecho-E "}" >& Gt $FILE
"Operation Procedure" # Modify permissions, script executable chmod u+x test-json.sh./test-json.sh "File contents" {"Name": "Xukai871105"}
6. SummaryLinux has a lot of tricks and needs to accumulate slowly. Add an oil to yourself.

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.