For example, if you wanted to list all of the files in the directories /usr and /usr2, you could type ls /usr*. If you were only interested in the files beginning with the letters b and e in these directories, you could type ls /usr*/[be]* to list them.
2.花括号扩展嵌套
It is also possible to nest the braces, as in b{ar{d,n,k},ed}s. This will result in the expansion bards, barns, barks, and beds. 此外花括号扩展要求至少有一个逗号 b{o}lt remains as b{o}lt. 3.
快捷键: CTRL-A and CTRL-E for beginning and end of line 通配符扩展又叫globbing: wildcard expansion is sometimes called globbing 变量命名时: varname=value
here must be no space on either side of the equal sign, and if the value is more than one word, it must be surrounded by quotes
echo 当用双引号时,当成一个整体的字符串;当用单引号时,是splits the string into words 然后打印,所有后面的空格就被截断了
Typically, several shell functions are defined within a single shell script. Therefore each function will need to handle its own arguments, which in turn means that each function needs to keep track of positional parameters separately. Sure enough, each function has its own copies of these variables (even though functions don't run in their own subshells, as scripts do); we say that such variables are local to the function. However, other variables defined within functions are not local (they areglobal), meaning that their values are known throughout the entire shell script. For example, assume that you have a shell script called ascript that contains this:
function afunc
{
echo in function: $0 $1 $2
var1="in function"
echo var1: $var1
}
If you invoke this script by typing ascript arg1 arg2, you will see this output:
var1: outside function
ascript: arg1 arg2
in function: ascript funcarg1 funcarg2
var1: in function
var1: in function
ascript: arg1 arg2
In other words, the function afunc changes the value of the variable var1 from "outside function" to "in function," and that change is known outside the function, while $1 and $2 have different values in the function and the main script. Notice that $0 doesn't change because the function executes in the environment of the shell script and $0 takes the name of the script.
当用local声明一个变量时,则是局部的,如果处于函数中与外部全局变量重名,则覆盖外部全局变量
function afunc
{
local var1
echo in function: $0 $1 $2
var1="in function"
echo var1: $var1
}
Now the result of running ascript arg1 arg2 is:
var1: outside function
ascript: arg1 arg2
in function: ascript funcarg1 funcarg2
var1: in function
var1: outside function
ascript: arg1 arg2 Figure 4-3 shows the scope of each variable in our new script. Note that afunc now has its own, local copy of var1, although the original var1 would still be used by any other functions that ascript invokes.
Figure 4-3. Functions can have local variables
5.
sort -nr $1 | head -${2:-10}
Here is how this works: the sort program sorts the data in the file whose name is given as the first argument ($1). The -n option tells sort to interpret the first word on each line as a number (instead of as a character string); the -r tells it to reverse the comparisons, so as to sort in descending order. The output of sort is piped into the head utility, which, when given the argument -N, prints the first N lines of its input on the standard output. The expression -${2:-10} evaluates to a dash (-) followed by the second argument if it is given, or to -10 if it's not; notice that the variable in this expression is 2, which is the second positional parameter. Assume the script we want to write is called highest. Then if the user types highest myfile, the line that actually runs is:
sort -nr myfile | head -10
Or if the user types highest myfile 22, the line that runs is:
sort -nr myfile | head -22 Expression Result
${path##/*/} long.file.name
${path#/*/} cam/book/long.file.name
$path /home/cam/book/long.file.name
${path%.*} /home/cam/book/long.file
${path%%.*} /home/cam/book/long 6.Command Substitution To find out detailed information about a command if you don't know where its file resides, type ls -l $(type -path -all command-name). The -all option forces type to do a pathname look-up and -path causes it to ignore keywords, built-ins, etc.
When in doubt, use single quotes, unless the string contains variables or command substitutions, in which case use double quotes 什么意思呢? cut -fN -dC filename, use this instead: awk -FC '{print $N}' filename mail $(who | cut -d' ' -f1) $后用括号扩起命令,可以是取值,类似重定向