诚然,vi等工具也可以在不离开shell的情况下编辑文件.可此类编辑器始终需要脱离命令行操作.
以下命令是纯粹在命令行中执行的,这相较于文本编辑器来说有以下好处:
- 操作更加快捷.
- 便于在bash编写的shell脚本中执行.
cat:
cat命令是使用频率最高的.是concatenate的简写. 多用于一次性显示所有文件内容.
$ cat file1
新建文件,并交互式键入文件内容
$ cat > file1 The text you want to type in. <Ctrl-D>
or
$ cat > file1 << EOF > The text you want to type in. > EOF
在交互模式下输入文件内容到已存在的文件尾部
$ cat >> file2 Here type your text appending to the end of file2. <Ctrl-D>
合并文件,并将合并后内容输出
$ cat file1 file2
合并文件,并将合并后内容写入到新建文件中
$ cat file1 file2 > newfile
将文件内容追加到另一个现有文件的尾部
$ cat file1 >> existing_file
echo
echo主要用来将文本标准化输出.
输出文本
$ echo Some string you want to output
输出系统的环境变量
$ echo $SHELL
新建文件,并将文本写入到文件中
$ echo The text will be added into newfile > newfile
将文本内容追加到现有文件的尾部
$ echo The text will be appended at the end of the existing file >> existing_file