Seeing The World As The Shell Sees It

来源:互联网 发布:科比0506常规赛数据 编辑:程序博客网 时间:2024/05/22 11:39

Here we will introduce one command:
echo:display a line of text

Expansion

For example “*”,can have a lot of meaning to the shell.The process that makes this happen is called expansion.

echo

echo is a shell built that performs a very simple task.example:

echo this is a text
echo *

can’t print ““,in privious character means match any characters in a filename.The shell expanse the “*” into something else before the echo command is executed.When the enter key is pressed,the shell expands any qualifying characters on the command line before the command is carried out.example:

echo [[:upper:]]*## will show all the file that beginning with an upper character
echo /usr/*/share## "*" expanse everything you can image~

want to expanse hiden file?,OK:

echo .*

but this command will show ‘.’ and ‘..’ this two.If I don’t want show current directory and parent directory,I can try:

echo .[!.]*

this will work correctly in most files.It still can’t include filenames with multiple leading periods,so if you want to show a correct listing of hidden files:

ls -A

Tilde Expansion

the ~ character means the home directory of the current user:

echo ~

if user ‘wuxiao’ has an account,then:

echo ~wuxiao## it is the same of 'echo ~' with 'wuxiao' user

Arithmetic Expansion

we can use the shell prompt as a calculator

echo $((2+2))

Arithmetic expansion use the form:

$((expression))

Arithmetic operators:
- +
- -
- *
- /
- ** :Exponentiation
example:

echo $(($((5**2))*3))echo $((5**2*3))echo $(((5**2)*3))##output is 75
echo ((5/2))##will display 2

Brace expansion

with brace,you can create multiple text strings from a pattern containing braces,example:

echo Front - {A,B,C} - Back## will show  'Front - {A,B,C} - Back'

using a range of integers:

echo Number_{1..5}##will show Number_1 Number_2 ... Number_5

also can be zero-padded like:

echo {001..6}##001 002 003 ... 006echo {z..a}##z y x ... a

The most common application is to make lists of files or directories to be created:

mkdir {2015..2017}-{01..12}

Parameter expansion

Use variable to store small chunks of data and to give each chunk a name.

echo $USERprintenv | lessecho $(ls)

Quoting

echo this is a                       test##this is a testecho the total is $100.00##the total is 00.00

Shell will remove the extra whitespace from the echo command’s arguments.example 2 shell will treat ‘$1’ as a parameter expasion.SO what should we do if we want to leave the origin output?

Use Double Quotes

ls -l "two words.txt"mv "two words.txt" two_words.txtecho "$USER "\$100.00" $(cal)"

Single Quotes

If we need to suppress all expansions,we use single quotes:

echo '23467567*&!~@$%}{}|?/\6346'

Escaping Characters ‘\’

Just like the function in C.Here are some frequent examples:
- \a:Bell
- \b:Backspace
- \n:NewLine
- \r:Carriage return
- \t:Tab

原创粉丝点击