bash shell (1) bash basics of linux review notes

1. The legal shell of the system and the function of /etc/shells

1.1. Check how many legitimate shells we can use on the current system

The legal shells that linux can use can view the file /etc/shells

[root@bogon etc]# cat /etc/shells
/bin/sh (replaced by /bin/bash)
/bin/bash (linux default shell)
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

 1.2. View the shell obtained by the current user by default, and view the file /etc/passwd, as shown below   

[root@bogon etc]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync

 

   As shown above, the last data in each line is the default shell you can get after logging in.

 

2. The built-in command of the bash shell: type

      In order to facilitate the operation of the shell, in fact, bash has "built-in" many commands, such as cd, umask and other commands, which are all built-in in bash.

      So how do we know whether this command comes from an external command (referring to other commands not provided by bash) or is built into bash? Use the type command to view it, use the following:

type [-tpa] name
parameter:
type: without any parameters, type will show whether the name is an external command or a bash built-in command
-t : When adding -t, type will display its meaning with the following words:
      file: represented as an external command;
      alias: indicates that the command is the name set by the command alias;
      builtin: indicates that the command is a built-in command of bash
-p : If the following name is an external command, the full file name will be displayed
-a : In the path defined by the PATH variable, all commands with name will be listed, including alias
Example 1: Querying whether the ls command is a built-in command of bash
[root@bogon etc]# type ls
ls is aliased to `ls --color=auto' ==" It can be seen that ls is a command alias
[root@bogon etc]# type -t ls
alias == "listed in a simplified way
[root@bogon etc]# type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls ==" lists the file where this command is located
[root@bogon etc]# type -t cd
builtin == "cd is a built-in command of bash  

 

3. The variable function of the shell

3.1. Display and setting of variables: echo, unset

Display of variables: echo

 

[root@bogon etc]# echo $JAVA_HOME
/usr/java/jdk1.7.0_55
[root@bogon etc]# echo ${JAVA_HOME}
/usr/java/jdk1.7.0_55

 3.2. Setting variables or modifying variables

 

Setting and modifying variables is so easy, just connect the variables with an equals sign (=), for example:

 

[root@bogon etc]# myname=ickes
[root@bogon etc]# echo $myname
ickes

 The rules for setting variables need to be paid attention to

1. Spaces cannot be directly connected to both sides of the equal sign, as follows is wrong

[root@bogon etc]# myname= ickes
bash: ickes: command not found

 2. The variable name can only be letters and numbers, but cannot start with numbers

 

 3. If there are spaces in the variable content, you can use double quotes (") or single quotes (') to combine the variable content, but pay attention

    Special characters in double quotation marks such as $, etc., can retain the original characteristics, as shown below   

 

[root@bogon etc]# test="lang is $LANG"
[root@bogon etc]# echo $test
long is en_US.UTF-8

 

   Special characters within single quotes are normal characters (plain text), as follows

 

[root@bogon etc]# test='lang is $LANG'
[root@bogon etc]# echo $test
long is $LONG

 4. You can use the escape character (\) to convert special characters such as $ into general characters

 

 5. In a series of commands, if you need other commands to provide information, you can use back single quotes (`command`) or $(command). The reverse single quotation mark is the key to the left of the 1 key. For example, to get the version of the current operating system kernel, as shown below:

[root@bogon etc]# version=`uname -r`
[root@bogon etc]# echo $version
2.6.32-358.el6.i686
[root@bogon etc]# version1=$(uname -r)
[root@bogon etc]# echo $version1
2.6.32-358.el6.i686

 

   What are the meanings of the backticks (``) and $() during command execution?

   A: In a series of commands, the commands within ` will be executed first, and the result returned by this command will be used as external input information`

  

6. All uppercase variables in the industry rules are environment variables, and the variables set by yourself are lowercase letters, which is convenient for judgment.

3.3, cancel variable: unset

[root@bogon etc]# myname=ickes
[root@bogon etc]# echo $myname
ickes
[root@bogon etc]# unset myname
[root@bogon etc]# echo $myname

[root@bogon etc]#

3.4, child process

What is a child process? That is to say, in the case of my current shell, to open another new shell, the new shell is the child process. Under normal circumstances, the custom variables of the parent process cannot be used in the child process. But after the variable is turned into an environment variable through export, it can be applied under the child process.

E.g:

[root@bogon ~]# name=ickes
[root@bogon ~]# bash enters subprocess
[root@bogon ~]# echo $name displays variables in subprocess
			   Of course nothing, because the child process can't reference the parent's variables
[root@bogon ~]# exit leave the child process
exit
[root@bogon ~]# export name Set the variable name as an environment variable
[root@bogon ~]# bash enters the subprocess again
[root@bogon ~]# echo $name display variable
ickes print it out, ghee ghee!
[root@bogon ~]# exit
exit

 

4. Functions and viewing of environment variables

4.1. Use evn to view environment variables, and the description of common environment variables

The env command can list all current environment variables

HOME: represents the home folder of the current user

SHELL: He told us which program is the shell currently used by this environment variable? Linux uses /bin/bash by default

HISTSIZE: This is related to "historical commands", that is, the commands we have executed can be recorded by the system, and the number of recorded records is specified by this variable

MAIL: When we use the mail command, the system goes back to the mailbox file read when we receive mail

PATH: The path variable of the executable file

LANG: This important thing is the language data, which is used in a lot of information.

RANDOM: This is a "random number" variable, for example as follows

 

[root@bogon ~]# echo $RANDOM
20801
 The system will randomly generate a random number from 0 to 32767

 

4.2. Use set to view all variables (including environment variables and custom variables)

I found a bunch of things, and I haven't used them specifically. When I use them, I will add them. The blog is good, and you can change it at any time.

4.3.export: Convert custom variables to environment variables

After talking about env and set, I now know that there are so-called environment variables and custom variables. What is the difference between the two directly?

Answer: The direct difference between the two is whether the variable will continue to be used by the child process .

Interpretation of parent process and child process

Answer: After you log in to linux and obtain a bash, your bash is an independent thread, and any commands you execute under this bash are derived from your bash, and those executed commands are is called a child process.

as the picture shows



 As shown in the figure above, we execute another bash under the original bash, and the resulting operating environment interface will run to the second bash (this is the subprocess), then the original bash will be in a suspended state (sleep) .If you want to go back to the original bash, you can only end the current subprocess (exit or logout).

How does this program concept relate to variables?

Answer: The relationship is very big, because the child process will only inherit the environment variables of the parent process, and the child process will not inherit the custom variables of the parent process , so the custom variables in your original bash will disappear after entering the child process. It cannot be referenced until the child process exits back to the parent process.

If you replace the custom variable with an environment variable, you can continue to access the above in the child process, so I won't be verbose here!

 


to be continued!

 

 

Guess you like

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