Some basic Bash Problems

Source: Internet
Author: User

Some basic Bash Problems
Some basic Bash ProblemsHere we will introduce some issues before and after bash is started, as well as some basic issues that need to be paid attention to when using bash.1. Bash IntroductionBash is a Shell program. It is the default Shell program in general Linux systems. Bash generally refers to the/bin/bash software. The number of available Shell programs in a Linux system can be found in the/etc/shells file. Which is the default Shell of the currently logged-on user? It can be seen from the last field of the row corresponding to this user in the/etc/passwd file. Alternatively, you can view it using echo $ SHELL.2. Internal and External commandsBash supports two types of commands: Internal commands and external commands. Internal commands are part of Bash, while external commands are a software independent of Bash. At the bash user prompt, you can enter internal and external commands. bash will analyze the commands and then execute the commands. Bash does not generate new processes when executing Internal commands, but bash uses the fork function to generate a new process when executing external commands, then the exec function is used to load the program of this external command to execute this external command. How can we identify whether a command is an internal command or an external command? Use the type command to differentiate. Type itself is an internal command. You can know whether the commandName command is an internal command or an external command.3. environment variables and User-Defined variablesEnvironment variables are visible to all processes (including bash processes and non-bash processes). Generally, they are combined with uppercase letters as variable names, which are also called global variables. Glossary: temporary and permanent environment variables. In fact, global variables are all exported through export. Temporary global variables are the variables exported using export at the bash command prompt or bash scripts. These variables will disappear after the machine is restarted, unless you execute the export command again or execute the corresponding scripts again. Permanent environment variables are written in the configuration file. These configuration files are re-executed every time the machine is restarted or even a bash is re-opened. Therefore, the global variables in these files always exist, it does not disappear because the machine is restarted. It seems to be permanent. In fact, it is re-export every time. User-Defined variables are visible to a single Bash. other subprocesses (including subbash processes and other subprocesses) generated by Bash that own this variable cannot see this variable, this is also called a local variable. The main difference between environment variables and User-Defined variables is that environment variables are inherited by subprocesses started by bash, while User-Defined variables are not inherited by bash subprocesses. Environment variables in the system can be viewed using the env and export commands. Entering env or export directly at the bash command prompt lists all environment variables of the system. Enter the set command under bash to view both the system environment variables and the User-Defined variables of bash, in addition, some listed variables are related to the bash operation interface.4. Execution method of shell scriptsBash script execution method. One is to analyze scripts and then execute scripts. The other is to start a new bash process, the new bash process is used to analyze and execute scripts. To analyze and execute scripts in this bash, run [zzl @ myhost Scripts] $ source test with the source command or by clicking the command. sh [zzl @ myhost Scripts] $. test. sh runs another bash process in two ways: [zzl @ myhost Scripts] $ bash test. sh [zzl @ myhost Scripts] $ chmod a + x test. sh (in test. use shebang to specify the shell to be used.) [zzl @ myhost Scripts] $. /test. sh5. Interactive execution of a single command in BashEnter a command at the bash command prompt, and bash will explain and execute the command. This method becomes interactive. Interactive execution of internal commands: After you enter an internal command, bash analyzes the command and runs it directly. Executing an internal command is equivalent to executing a bash function. Interactive execution of External commands: After you enter a command on the command line, bash analyzes the validity of the command and then generates a new process, and execute the exec command in the new process.6. Execution Process of shell scriptsThe execution process of scripts in bash is related to the execution method. When source or vertex commands are used, the script is executed by the bash command. If the script is executed by the bash command or directly, another subbash process is used to execute the scripts. So how does a bash process or bash sub-process execute scripts? [Zzl @ myhost Scripts] $ source test. sh. sh is read as a command line parameter, and then read from test. sh. [Zzl @ myhost Scripts] $ bash test. sh after the command is executed, bash will start another sub-bash process. The sub-bash reads test. sh as its own command line parameter. Then, the sub-bash will read one from test. sh, analyze one, and execute one. In this process, if you read an internal command, you can directly call your own corresponding function. If you read an external command, it will fork a sub-process, then execute the program that exec this external command in the sub-process. Until all the commands in test. sh are executed, the sub-bash ends and is returned to the current bash.7. Bash operating environmentThe welcome information before logging on to the system is stored in/etc/issue, and the information you want to display after logging on is stored in/etc/motd. Login shell: Enter bash only after entering the complete logon process with the username and password. This bash is called login shell. Non login shell: when entering bash, This bash is non login bash without the need to go through the logon process. For example, in X Window, open a terminal and enter bash, that is, non login shell. In a command prompt, enter bash, bash entered later is also a non-login shell. Before entering the login shell, read/etc/profile ,~ /. Bash_profile or ~ /. Bash_login or ~ /. Profile and other configuration files, while in ~ /. Bash_profile will be executed again ~ /. Bashrc file. Non-login shell reads only ~ /. Bashrc file. Login shell will read the logs when logging out and exiting ~ /. Bash_logout configuration file content, while non-login shell won't read. ~ The/. bash_history file records the commands used by login shell in the previous and previous sessions. The above basic questions are clarified, and many problems will be clarified during the use of bash. The remaining problem is the problem in Bash, that is, how to learn the problem of bash Script Programming and the usage of various commands.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.