foo=bar echo foo # 失效 foo = bar # echo单引号和双引号的区别 echo "value is $ foo" # value is bar echo 'value is $foo' # value is $foo echo "we are in $(pwd)"
.sh中的$
# creates a directory and cds into it. mcd () { mkdir -p "$1" cd "$1" }
$0 - Name of the script
$1 to $9 - Arguments to the script. $1 is the first argument and so on.
$@ - All the arguments
$# - Number of arguments
$? - Return code of the previous command(上一次的错误代码,echo $?,返回值为0代表正确)
2$ -Process Identification number for the current script (两个美元一起,把’2’换成’$’,我这里显示有问题)
!! - Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions, then you can quickly execute it with sudo by doing sudo !!
$_ - Last argument from the last command. If you are in an interactive shell, you can also quickly get this value by typing Esc followed by .
grep命令用于查找文件里符合条件的字符串,grep foobar mcd.sh
true echo $? # 0 false echo $? #1
false || echo "Oops, fail" # Oops, fail
true || echo "Will not be printed" #
true && echo "Things went well" # Things went well
false && echo "Will not be printed" #
false ; echo "This will always run" # This will always run,分号代表两个不关联的语句
echo "Starting program at $(date)" # Date will be substituted
echo "Running program $0 with $# arguments with pid $$"
for file in $@; do grep foobar $file > /dev/null 2> /dev/null # When pattern is not found, grep has exit status 1 # We redirect STDOUT and STDERR to a null register # since we do not care about them if [[ $? -ne 0 ]]; then echo "File $file does not have any foobar, adding one" echo "# foobar" >> "$file" fi done
ls *.py #打印所有含有.py的文件名 ls project? #打印所有"project"后加一个字符的文件名
# Find all directories named src find . -name src -type d # Find all python files that have a folder named test in their path find . -path '**/test/**/*.py' -type f # Find all files modified in the last day find . -mtime -1 # Find all zip files with size in range 500k to 10M find . -size +500k -size -10M -name '*.tar.gz'
# 还可以找到特定的文件然后执行命令 # Delete all files with .tmp extension find . -name '*.tmp' -exec rm {} \; # Find all PNG files and convert them to JPG find . -name '*.png' -exec convert {} {.}.jpg \;
# Find all python files where I used the requests library rg -t py 'import requests' # Find all files (including hidden files) without a shebang line rg -u --files-without-match "^#!" # Find all matches of foo and print the following 5 lines rg foo -A 5 # Print statistics of matches (# of matched lines and files ) rg --stats PATTERN