[Linux] How to use the echo command

Begin

Outputs the method that you often use for one of the Linux commands, the echo command.

What is the echo command?

This is a command that can do something like this.

--Display of character string on the screen --Display of command execution result --Output the string to a file --Add character string to file

How to use the echo command (example)

Display of character string

Displays the specified character string.

command


echo "String"

Command execution example

Execution example


[root@tspweb01 test]# echo aaaaa
aaaaa

Displaying command execution results

Displays the command execution result.

command


echo $?

--Command execution result

Execution result Contents
0 True(Command execution successful)
1 False(Command execution failed)
127 Command not found

Command execution example

--True (success)

Execution example(True)


[root@tspweb01 test]# true
[root@tspweb01 test]# echo $?
0

--False

Execution example(False)


[root@tspweb01 test]# false
[root@tspweb01 test]# echo $?
1

--Command not found

Execution example(Command not found)


[root@tspweb01 test]# wh
-bash: wh:Command not found
[root@tspweb01 test]# echo $?
127
[root@tspweb01 test]#

Output string to file

Outputs the specified character string to a file.

command


echo "String" >file name

Command execution example

Execution example


[root@tspweb01 test]# echo "testtest" > test.txt
[root@tspweb01 test]# cat test.txt
testtest
[root@tspweb01 test]#

Add character string to file

Adds the specified character string to the file.

command


echo "String" >>file name

Command execution example

[root@tspweb01 test]# cat test.txt
testtest
[root@tspweb01 test]# echo "testtest" >> test.txt
[root@tspweb01 test]#
[root@tspweb01 test]# cat test.txt
testtest
testtest
[root@tspweb01 test]#

Note

Please be careful not to confuse > with >> during actual work. (The meaning is completely different.)

--> : Overwrite the entire file -->> : Add one sentence to the last line of the file

Be sure to use the cp command to make a backup when working on important files.

Summary

You can do this with the echo command.

--Display of character string on the screen --Display of command execution result --Output the string to a file --Add character string to file

How about applying it to a bash script?

reference

Detailed summary of echo commands [Linux command collection] Unexpectedly convenient command true / false

Recommended Posts

[Linux] How to use the echo command
How to use the Linux grep command
Linux user addition, how to use the useradd command
(Remember quickly) How to use the LINUX command line
How to use the generator
How to use the decorator
How to use the grep command and frequent samples
How to use the optparse module
How to use MBDyn (command setting)
[linux] kill command to kill the process
How to use the ConfigParser module
Add Linux user, how to use useradd command (password specification)
How to use the Spark ML pipeline
[python] How to use __command__, function explanation
How to calculate Use% of df command
How to operate Linux from the console
How to use CUT command (with sample)
How to use the IPython debugger (ipdb)
3 best ways to use the less command
How to output the output result of the Linux man command to a file
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use the C library in Python
How to use MkDocs for the first time
How to use the graph drawing library Bokeh
How to use the Google Cloud Translation API
How to operate Linux from the outside Procedure
How to use the NHK program guide API
[Algorithm x Python] How to use the list
How to create a shortcut command for LINUX
[C language] How to use the crypt function on Linux [Password hashing]