Shell's echo command is similar to PHP's echo command, both of which are used for string output. Command format:
echo string
- Display normal strings:
echo "It is a test"
#这里的双引号完全可以省略,以下命令与上面实例效果一致:
echo It is a test
- show escape characters
echo "\"It is a test\""
#同样,双引号也可以省略
echo \"It is a test\"
- display variable
The read command reads a line from standard input and assigns the value of each field of the input line to a shell variable
read name
echo "$name It is a test"
The above code is saved as test.sh, name receives the variable of standard input, the result will be:
./test.sh
OK #标准输入
OK It is a test #输出
4. Display newline
echo -e "OK! \n" # -e 开启转义
echo "It is a test"
Output result:
OK! It is a test
5. Display does not wrap
echo -e "OK! \c" # -e 开启转义 \c 不换行
echo "It is a test"
Output result:
OK! It is a test
6. The display results are directed to the file
echo "It is a test" > myfile
7. Output the string as it is, without escaping or taking variables (with single quotes)
echo '$name\"'
Output result:
$name\"
8. Display command execution results
echo `date`
Note: Backticks ` are used here , not single quotes ' .
The result will show the current date
Thu Jan 31 14:50:46 CST 2023