bash shell of linux review notes (2) bash basics

Please reprint from the source: http://eksliang.iteye.com/blog/2104329

1. The language variable (locale) that affects the displayed results

 1.1 The locale command is to check how many languages ​​the current system supports. The command is used as follows:

[root@localhost shell]# locale
LANG = en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
......

1.2 How to adjust the language variables of the system?

Answer: The default language of the system is defined in the file /etc/sysconfig/i18n, we can modify this file to adjust the language of the system 

[root@localhost locale]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"

 

2. Variable keyboard read, array and declaration: read, array, declare

2.1.read

To read variables from keyboard input, use the command read

grammar:
read [-pt] var
parameter:
-p: can be followed by a prompt.
-t: The "seconds" that can be followed by waiting.
Example 1 Let the user enter their name from the keyboard
[root@localhost locale]# read name
ickes After pressing the enter key, the cursor will wait for your input                    
[root@localhost locale]# echo $name
The content just entered by ickes is used as the value of the variable name
Example 2 Let the user enter their own name within 30 seconds
[root@localhost locale]# read -p "you name is:" -t 30 name
you name is:xialiang has a hint
[root@localhost locale]# echo $name
The content just entered by xaliang is used as the value of the variable name

 

 2.2.declare、typeset

 declare or typeset have the same function, that is to declare the type of the variable, I usually choose to use declare

 The usage of declare:

 

grammar:
declare [-aixr] var
parameter:
-a: Define the variable named var as an array type
-i: Define the variable named var as an integer (int) type
-x: The usage is the same as export, turning the following var into an environment variable
-r: Make the variable named var read-only
Example 1: Let the variable sum accumulate 100+200+300
[root@localhost locale]# sum=100+200+300
[root@localhost locale]# echo $sum
100+200+300 What is he doing? It turns out that Linux defaults, if the type of the variable is not specified, then the type of the variable is a string
[root@localhost locale]# declare -i sum=100+200+300
[root@localhost locale]# echo $sum
600
Example 2: Turn sum into an environment variable
[root@localhost locale]# declare -x sum
[root@localhost locale]# export | grep sum
declare -ix sum="600" Did you see that, it really includes the declaration of i and x
Example 3: Make sum a read-only attribute that cannot be changed
[root@localhost locale]# declare -r sum
[root@localhost locale]# sum=100
-bash: sum: readonly variable Sure enough, change this variable
Example 4: Make sum a non-environment variable
[root@localhost locale]# declare +x sum turns - into + and it becomes a cancellation operation
[root@localhost locale]# declare -p sum -p can list the types of variables individually
declare -ir sum="600"
But note: -r is a read-only attribute, once set, it cannot be canceled, usually you need to log out and log in again
 Note: By default, bash has several basic definitions for variables

 

 1). The variable type defaults to a string (as can be seen from example 1);

 2). Numerical operations in the bash environment can only reach integer types by default, so 1/3 of the result is 0

2.3. Arrays

The syntax is quite simple: var[index]=content, as shown below, is it so simple,

 

[root@localhost locale]# var[0]=1
[root@localhost locale]# var[1]=ickes
[root@localhost locale]# echo ${var[0]}
1
[root@localhost locale]# echo ${var[1]}
ickes
Generally speaking, it is recommended to read directly in the form of ${array}, which will be more correct.
 3. Command alias settings: alias, unalias

 

 This is fairly easy to understand, illustrated by an example: it's that simple

 

[root@localhost ~]# alias l='ls -al'
[root@localhost ~]# l
Used alone, you can see which commands are named aliases
[root@localhost ~]# alias
alias cp='cp -i'
alias l='ls -al'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls = 'ls --color = auto'
alias mv='mv -i'
alias rm = 'rm -i'
 To cancel the command alias is to use
[root@localhost ~]# unalias l
  4. History commands: history bash has a service that provides command history. So how do we query the commands we have executed? Answer: Use the history command  
grammar:
history [n]
history [-c]
histroy [-raw] histfiles
parameter:
n: number, lists the meaning of the most recent n commands
-c: Clear all history content in the current shell
-a: Add the newly added history command to the histfiles file. If the histfiles file is not added, it will be written to ~/.bash_history by default.
-r: Read the contents of histfiles into the history memory of the current shell
-w: Write the current history memory content to historyfiles
Example 1: List all the history memories in the current memory
[root@localhost ~]# history
The content is too much, the length is too large, I will not show it
Example 2: List the most recent 3 pieces of data
[root@localhost ~]# history 3
 1134  history[root@localhost ~]# history 3
 1135  clear
 1136  history 3
Example 3: Write the current history record in memory to the hist.txt file
[root@bogon ~]# history -w hist.txt
 Under normal circumstances, the reading and recording of historical commands is as follows: When we log in to the linux host with bash, the system will actively read the previously executed commands from ~/.bash_history in the main folder. After logging in to the host, a total of 100 commands have been executed. When I log out, the system will update a total of 1000 historical commands from 101 to 1000 to ~/.bash_history. That is to say, when I log out, the history command will record the most recent HISTSIZE items to my log file. Of course, you can also use history -w to force immediate write.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486370&siteId=291194637