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
- Usage: echo [short options] ... [String] ...
- Or: Echo Long option
- Echoes a STRING back to standard output.
- -n do not trailing line breaks
- -e enable escape function to interpret backslashes
- -e disables the escape function that interprets backslashes (default)
- --HELP Display this help message and exit
- --version display version information and exit
- If-e is available, the following sequence is recognized:
- \ \ counter Slash
- \a Ring Ring
- \b Backspace
- \c no longer produces new output
- \e Escape character
- \f Page Change
- \ n New Line
- \ r Enter
- \ t Horizontal tab
- \v Vertical Tab
- \0nnn bytes are expressed in octal NNN (1 to 3 bits)
- \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
- #!/bin/bash
- echo "Raspberry" > Test.txt
- 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
- #!/bin/bash
- Echo-e "{" > Test-json.txt
- Echo-e "\t\" name\ ": \" xukai871105\ "" >> Test-json.txt
- 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
- #!/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 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