Echo is an internal command of the shell that prints the specified string on the screen. Command format:
- Echo Arg
You can use echo to achieve more complex output format control.
Show escape characters
- echo "\"It is a test\ ""
The result will be:
"It is a test"
Double quotes can also be omitted.
Show variables
- Name="OK"
- echo "$name It is a test"
The result will be:
OK It is a test
The same double quotation marks can also be omitted.
If the variable is connected to another character, you need to use curly braces ({}):
- Mouth=8
- echo "${mouth}-1-2009"
The result will be:
8-1-2009
Show line breaks
- echo "ok! \ n"
- Echo "It is a test"
Output:
Ok!
It is a test
Show No Line breaks
- echo "ok! \c"
- Echo "It is a test"
Output:
Ok! It si a test
Show results redirected to file
- Echo "It is a test" > MyFile
Output string AS-is
If you want to output the string as-is (without escaping), use single quotation marks. For example:
- echo ' $name\ '
Show command Execution results
- Echo ' Date '
The result will show the current date
As can be seen from the above, double quotes are dispensable, single quotes are mainly used in the original output.
Shell Script Learning 14 Shell echo command