在这一章节中,从四个方面进行介绍:特别的文件,别名,选项,参数。

特殊文件

  在用户目录下有几个特殊的文件,可以用ls -a 来查看这些隐藏的系统文件。.bash_profile只重要的文件,经常用于设置用户环境。这个文件将在用户login的时候执行。全局(所有用户使用的)为/etc/profile。通长我们将新的配置附加在该文件的末尾,修改后需要重新登录方才生效,或者使用source命令来执行这些特殊文件,即source .bash_profile

  现在的linux一般都是使用.bash_profile,如果没有系统会去找.bash_login,次去找.profile。另外还有.bashrc文件,这实在调用subbash的时候系统执行的,也即是我们敲入bash 来开启新的bash环境,这在实际中是很少用的。.bash_logout顾名思义,是在logout是调用。

别名

  在环境文件中,会设置一些别名,当然也可以在正常的bash命令行使用。通过别名,我们可以用我们便于记忆或者经常使用的其他操作系统的命令来替代linux的命令,也可以将很长一串命令用比较简单的单词来替代。格式如下:

alias name =command

  请注意在“=”的前后是不允许留有空格的。例如用search来代替查找命令grep,alias search=grep,如果command中是含有空格的,需要用引号来表示,例如alias ls=’ls –color’。当然我们需要注意不要将通配符这些特殊符号用于name。我们可以在command中放入一个已经alias的别名,这是允许的。bash是禁止loop的,如果碰到,它会停止进一步翻译,例如上面的例子ls作为ls -color,而这两个ls在逻辑上可以产生loop,但bash不会允许这种情况的发生。虽然这样,我们要防止在多个alias上自己给自己设置环回设定,简洁、清晰,是一个程序员的准则。

  别名只能用于命令行的开始,例如alias mywork=’cd /home/wei/project/mywork’,我们嵌入mywork,就可以直接进入指定的命令,但是我们不能alias mywork=/home/wei/project/mywork,然后cd mywork,别名是不会去解析不作为第一位的命令的。这种情况可以用export的方式来处理。

alias:例如当前alias的的列表
alias name :例如给name的真正含义
unalias name :取消该name的别名捆绑

选项

  我们可以通过set –o optionname 来打开某个环境选项,用set +o optionname 来关闭它,注意是-表示on,+表示off,这是应为-比较容易按,而+需要按两个键。可以用set -o来查看当前环境的开关情况,例如为了禁止CTRL-D引发logout,我们可以打开ignoreeof,及set –o ignoreeof。我觉得有很多这样的环境设定参数,但是其实很少使用,需要的时候可以通过man set来确定一下就可以。另外可能比较常用的是noclobber,禁止通过>的重定向方式重写一个已有的文件。在bash 2.0,提供shopt来设置,也提供了一些来设置和unset的方式,但是我不认为需要去记忆这些,在实际应用上是很少使用的。这里就不记录。

参数设定

  相对环境的设定,参数的设定更有有意思,set或者shopt是实际上很少使用的,而用户的环境变量设定,例如JAVA是lib或者bin的指定是通过.bash_profile来设定的。参数设置也就是varname =value ,通常varname都是大写,例如JAVADIR,我们查看一个varname,常用的方式就是echo,例如我们已经设定了TT=’test for it’,可以通过echo $TT或者echo “$TT”来查看。如果没有,就给出空行。

单引号和双引号

  我们来看看下面的例子:varname=alice;echo "$varname"和echo $varname,都显示alice,而echo ‘$varname’则显示$varname,这说明在双引号下是解析参数的,而单引号则表示一串字符不解析里面的内容。

在看一个例子varname=’four space    here’,如果echo "$varname",则显示four space    here,如果echo $varname,等同于echo four space    here,显示four space here。双引号内,表示一个整体,作为一个word出现,如果我们echo "four space    here"也一样在here之前有多个空格。

  在参数设定中分清单引号和双引号是必须的。

内置参数

  Linux系统包含一些内置参数,我们可以通过修改这些参数来改变配置。在我们自定义的参数名中要避免与之重名。

下面是history相关的部分参数:

  • HISTCMD记录单前是第N个命令,可以echo来查看一下。
  • HISTFILESIZE表示记录在文件中历史命令的数目,也即文件的最大行数;
  • HISTSIZE表示记录在内存中最大的历史命令的数目。
  • HISTIGNORE:表示不记录某些命令,例如HISTIGNORE=l*:&,表示不记录l开头的命令,以及不记录重复的命令(&表示重复命令)
  • HISTTIMEFORMAT:如果为null,则不显示时间,我们可以为之设定时间格式,例如HISTTIMEFORMAT=”%y/%m/%d %T “,请注意这里使用的是双引号。

时间格式

Format

Replaced by

%a

The locale's abbreviated weekday name

%A

The locale's full weekday name

%b

The locale's abbreviated month name

%B

The locale's full month name

%c

The locale's appropriate date and time representation

%C

The century number (the year divided by 100 and truncated to an integer) as a decimal number [00-99]

%d

The day of the month as a decimal number [01-31]

%D

The date in American format; the same value as %m/%d/%y.

%e

The day of the month as a decimal number [1-31]; a single digit is preceded by a space

%h

The same as %b

%H

The hour (24-hour clock) as a decimal number [00-23]

%I

The hour (12-hour clock) as a decimal number [01-12]

%j

The day of the year as a decimal number [001-366]

%m

The month as a decimal number [01-12]

%M

The minute as a decimal number [00-59]

%n

A newline character

%p

The locale's equivalent of either a.m. or p.m

%r

The time in a.m. and p.m. notation; in the POSIX locale this is equivalent to %I:%M:%S %p

%R

The time in 24-hour notation (%H:%M)

%S

The second as a decimal number [00-61]

%t

A tab character

%T

The time (%H:%M:%S)

%u

The weekday as a decimal number [1-7], with 1 representing Monday

%U

The week number of the year (Sunday as the first day of the week) as a decimal number [00-53]

%V

The week number of the year (Monday as the first day of the week) as a decimal number [01-53]; if the week containing 1 January has four or more days in the new year, then it is considered week 1—otherwise, it is the last week of the previous year, and the next week is week 1

%w

The weekday as a decimal number [0-6], with 0 representing Sunday

%W

The week number of the year (Monday as the first day of the week) as a decimal number [00-53]; all days in a new year preceding the first Monday are considered to be in week 0

%x

The locale's appropriate date representation

%X

The locale's appropriate time representation

%y

The year without century as a decimal number [00-99]

%Y

The year with century as a decimal number

%Z

The timezone name or abbreviation, or by nothing if no timezone information exists

%%

%

 

  书中还介绍了Mail相关的部分参数,但是实际上我们已经很少mail,而是使用想雷鸟,189邮箱这类邮件客户端或者web mail,所以相关的参数也不再此摘录。

  这里介绍了一个通过“\”来见长命令分段的方式,例子如下:其实如果双引号只要左引号,无右引号,也必须等右引号方才结束,下面的例子也可以不用双引号

$echo “hello ,\
>my friend”
hello ,my friend

提示符

  在linux中,提示符通常为$,已经root用户的#,这个和具体的linux版本有关系,我们也可以设置我们的提示符,例如加上用户名,时间等等。在Linux中与4个提示符,PS1,PS2,PS3和PS4。PS1是缺省的提示符。例如我们需要改为用户名$的方式,可以重新设置PS1,即PS1="\u$",如果我们还希望加上时间,设为PS1="\u-\A$ "。

  PS2是命令尚未输完,第二上的换行继续的提示符,通常为>。例如我们上面介绍的分段方式。PS3和PS4用于编程和debug。在以后的章节中介绍。下表为提示符的设置内容。

 

 

Command

Meaning

\a

The ASCII bell character (007)

\A

The current time in 24-hour HH:MM format

\d

The date in "Weekday Month Day" format

\D {format }

The format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation; the braces are required

\e

The ASCII escape character (033)

\H

The hostname

\h

The hostname up to the first "."

\j

The number of jobs currently managed by the shell

\l

The basename of the shell's terminal device name

\n

A carriage return and line feed

\r

A carriage return

\s

The name of the shell

\T

The current time in 12-hour HH:MM:SS format

\t

The current time in HH:MM:SS format

\@

The current time in 12-hour a.m./p.m. format

\u

The username of the current user

\v

The version of bash (e.g., 2.00)

\V

The release of bash ; the version and patchlevel (e.g., 2.00.0)

\w

The current working directory

\W

The basename of the current working directory

\#

The command number of the current command

\!

The history number of the current command

\$

If the effective UID is 0, print a #, otherwise print a $

\nnn

Character code in octal

\\

Print a backslash

\[

Begin a sequence of non-printing characters, such as terminal control sequences

\]

End a sequence of non-printing characters

 

  一般而言我们不会去修改他们,除非有特殊需求,所以没有必要去记忆这些系统参数名,需要的时候再查。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注