想用echo 命令把修改完的一段字符写入文件,发现换行符被当作正常字符处理了。一查发现默认是特殊字符是不转义的。
在shell脚本中,echo常用于显示消息或输出其他命令的结果。
echo[-neE] [参数]
在使用echo命令时有一些要考虑的重要点:
echo Hello, World!
输出:
Hello, World!
echo 'Hello "Linuxize"'
或
echo"Hello \"Linuxize\""
输出:
Hello "Linuxize"
echo "I'm a Linux user."
或
echo $'I\'m a Linux user.'
输出:
I'm aLinux user.
echo-e "You know nothing, Jon Snow.\n\t- Ygritte"
输出:
You know nothing, Jon Snow.
??? -Ygritte
echo The PHP files are: *.php
输出:
The PHP files are: index.php contact.php functions.php
echo-e 'The only true wisdom is in knowing you know nothing.\nSocrates'>> /tmp/file.txt
如果file.txt不存在,该命令将创建它。使用>时,文件将被覆盖,而使用>>将输出追加到文件。
使用cat命令查看文件内容:
cat/tmp/file.txt
输出:
The only truewisdom isinknowing you know nothing.
Socrates
echo $USER
echo "The date is: $(date +%D)"
输出:
The date is: 04/17/19
echo -e "\033[1;37mWHITE"
echo -e "\033[0;30mBLACK"
echo -e "\033[0;34mBLUE"
echo -e "\033[0;32mGREEN"
echo -e "\033[0;36mCYAN"
echo -e "\033[0;31mRED"
echo -e "\033[0;35mPURPLE"
echo -e "\033[0;33mYELLOW"
echo -e "\033[1;30mGRAY"
0是正常,其他是异常
返回码?? ???? | 表示意思 |
0 | successful termination |
1 | Catchall for general errors |
2?????? | Misuse of shell builtins (according to Bash documentation) |
126 | Command invoked cannot execute |
127?? | ?"command not found" |
128+n? | Fatal error signal "n" |
130?? | ?Script terminated by Ctrl-C |
255*? | ?Exit status out of range |
其他参考文件定义:
/usr/include/sysexits.h
#define EX_OK?????????? 0?????? /* successful termination */
#define EX__BASE??????? 64????? /* base value for error messages */
#define EX_USAGE??????? 64????? /* command line usage error */
#define EX_DATAERR????? 65????? /* data format error */
#define EX_NOINPUT????? 66????? /* cannot open input */
#define EX_NOUSER?????? 67????? /* addressee unknown */
#define EX_NOHOST?????? 68????? /* host name unknown */
#define EX_UNAVAILABLE? 69????? /* service unavailable */
#define EX_SOFTWARE???? 70????? /* internal software error */
#define EX_OSERR??????? 71????? /* system error (e.g., can't fork) */
#define EX_OSFILE?????? 72????? /* critical OS file missing */
#define EX_CANTCREAT??? 73????? /* can't create (user) output file */
#define EX_IOERR??????? 74????? /* input/output error */
#define EX_TEMPFAIL???? 75????? /* temp failure; user is invited to retry */
#define EX_PROTOCOL???? 76????? /* remote error in protocol */
#define EX_NOPERM?????? 77????? /* permission denied */
#define EX_CONFIG?????? 78????? /* configuration error */