- 使用$(command),将command的返回值作为另一个命令的组成部分
grep $(head -n1 a.txt) a.txt - daily & sophisticated shell built-in commands
12341. compgen -b:display all built-in commands (completion of command)2. type command: determine whether the command is a built-in command3. pushd/popd: add/remove directories from stack4. help command: display the help message of the command - sed [stream editor]
1sed '$,$ d' input > output #删除最后一行 - $’string’
123456789https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.htmlWords of the form $'string' are treated specially. The word expands tostring, with backslash-escaped characters replaced as specified by theANSI C standard.alias s=$'ls | perl -ne \'chomp();my $str = $_;print "$str\n"\' | sort -k4'#1.单引号内继续用单引号:解决方案:$'string',string内的单引号用反斜线转义#2.'\n'并未被传递给perl,传递给perl的是一个有换行的字符串。#因为perl支持一句话写在多行,所以这里才能没问题
- set -x
1234567891011121314151617181920212223set指令能设置所使用shell的执行方式,可依照不同的需求来做设置<strong> +<参数> 取消某个set曾启动的参数。</strong>-a 标示已修改的变量,以供输出至环境变量。-b 使被中止的后台程序立刻回报执行状态。-C 转向所产生的文件无法覆盖已存在的文件。-d Shell预设会用杂凑表记忆使用过的指令,以加速指令的执行。使用-d参数可取消。-e 若指令传回值不等于0,则立即退出shell。-f 取消使用通配符。-h 自动记录函数的所在位置。-H Shell 可利用"!"加<指令编号>的方式来执行history中记录的指令。-k 指令所给的参数都会被视为此指令的环境变量。-l 记录for循环的变量名称。-m 使用监视模式。-n 只读取指令,而不实际执行。-p 启动优先顺序模式。-P 启动-P参数后,执行指令时,会以实际的文件或目录来取代符号连接。-t 执行完随后的指令,即退出shell。-u 当执行时使用到未定义过的变量,则显示错误信息。-v 显示shell所读取的输入值。-x 执行指令后,会先显示该指令及所下的参数。 - I/O重定向
12345678910>| filename:即使noclobber选项已开启,仍然强制将标准输出写到文件filename之中,即将filename文件覆盖掉。noclobberWhen setting 'noclobber' overwriting the contentof an existing file by the '>' redirection operatorwill not be possible. You might have typed '>' toredirect the output of a certain command to anexisting file, while you intended to use '>>'redirection operator. - 在双引号内使用单引号,不需要加反斜线;使用双引号需要加反斜线;
- eval
- xargs
- awk [Aho Weinberger Kernighan]
- truncate last line
12345678if [ $# == 0 ];thenecho "USAGE:sh $0 filename is_there_newline"exit 0fiLAST=$(tail -n 1 $1)let TRUNCATE_SIZE="${#LAST} + $2"truncate -s -"$TRUNCATE_SIZE" $1 - 12