Linux Shell programming from the initialization file

Source: Internet
Author: User
Tags color representation
I. Preface: This article mainly describes the initial file and the reading sequence of each file in BASH ShellScript, LOGIN, and terminal software startup, the contents of the initial files will be described in detail. Ii. Overview: The standard Shell in Linux is Bash. Bash is not only used to explain and implement ShellScript, but also to control user logon, term and other end-to-end software commands accepted by the command line for processing and other aspects play an important role

I. Preface:

This article mainly describes the initial file and the reading sequence of each file during BASH ShellScript, user LOGIN, and end-to-end software startup, and details the content of the initial file.

Ii. Overview:

The standard Shell in Linux is Bash. Bash is not only used to explain and implement ShellScript, but also used to control user logon, term and other end-to-end software commands accepted by the command line for processing and other aspects play an important role. This Shell is called an Interactive Shell because it accepts the user's command (keyboard or other) and then displays the result on the screen after processing.

In a conversational Shell like this, you can define aliases and set environment variables. You can also change some end-to-end settings based on your own interests, such as LS color representation, change your root directory to %, #. You can use the BackSpace key to remove the root directory. If such processing is described in the initial file, it can be automatically executed at Shell startup. These initial files can be stored in their own root directory and customized according to their preferences. Of course, some common settings can be placed in the initial file of all users.

If you want to set your Linux system to be suitable for your use (this is the advantage of Linux), you must process the various initial processes of Linux Files, I have some knowledge about the different functions of each file. This article will describe it in detail.

Iii. Various initial files used by SHELL

The initial files processed during Shell startup can be roughly divided into two types: LoginShell started at LOGIN and second-level SHELL awakened at startup of other files. The contents of the initial files are all written in ShellScript, which is automatically read at Shell startup (or awakened by other files) and interpreted and implemented by Shell itself, therefore, the first line of the initial file must be [#! /Bin/sh] or [#! /Bin/bash], and do not add implementation attributes.

This section describes in detail the following aspects:

. LoginShell exploitation of initial files

. Initial files used by second-level Shell

. Reasonable utilization of the two types of files

3.1 initial files used by LoginShell

After you enter the username and password in Login, the Shell described in [/etc/passwd] will be started. The standard SHELL in LINUX is Bash, if you want to confirm the SHELL you are using, enter

 

       
        $ grep "^whoami:" /etc/passwd
       

The result is separated. The first section is the user name, and the last section is the LoginShell. Now let's take a look at the above command. whoami is the result replacement after the implementation of whoami, that is, your username. [^] is a regular expression, indicating what the meaning of the start is. For example, if your username is [john], this command retrieves and represents a line starting with [john:] From the file [/etc/passwd.

After being started as the Bash of LoginShell, the two files are continuously read and implemented by LoginShell. The first is the initial file [/etc/profile] shared by all users, followed by [. bash_profile] After Linux is installed, it is usually set in the root directory of each user. bash_profile file. Is there any file in your root directory? Use [ls-a] to confirm. If not, refer to the appendix in this section for how Bash handles it.

/Etc/profile ,. the typical processing content in bash_profile is the setting of important environment variables such as PATH and USER, the restriction of croe file size at the end of program exception, and the Read and Write attributes of new files. The specific/etc/profile and. bash_profile file will be detailed later.

3.2 initial files used by second-level Shell

The Shell started when software such as kterm of X Window System is started can also accept user commands, so it is also called conversational Shell when Level 2 conversational Shell is started, will automatically read. bashrc is interpreted and executed by the called Shell. Generally, all user-defined settings are described in/etc/bashrc, but/etc/bashrc cannot be read automatically. It must be indicated by. bashrc before it can be read. You can use the Bash source command or [.] to implement it, as described below in. bashrc:

 

       
        1: if [ -f /etc/bashrc ]; then 2: . /etc/bashrc 3: fi
       

Note: "-f" is the condition for determining whether a file exists.

/Etc/bashrc ,. the typical processing content in bashrc is: setting general environment variables, setting combination variables [PSI], alias definition, definition of Shell Han number, and change of end-to-end settings.

3.3 reasonable separate use of the two initial files

There are two initial files in the root directory of each user :. bash_profile and. bashrc, if the same content is written in both files, it will cause unnecessary repetition and modification troubles. How can we reasonably use it separately? The basic principle is:

* If there is no conflict in. bashrc, it should be described in. bashrc as much as possible to arouse. bashrc from. bash_profile.

* If you execute a one-time command at Login and a second-level dialog Shell at startup, the command that causes bad effects is described in. bash_profile, which invokes. bashrc in. bash_profile. The method is the same as reading/etc/bashrc from. bashrc, which is described as follows in. bash_profile:

 

       
        1: if [ -f ~/.bashrc ]; then 2: . ~/bashrc 3: fi
       

Appendix:

. Bash_login and. profile if the root directory does not exist. bash searches for the bash_profile file in the root directory. bash_login, if. if bash_login does not exist, Bash will continue searching in the root directory. profile file. In fact, these files are all from C shell (. login) And Bourne shell (. profile). They are all the initial files that only LoginShell uses. The difference is that. profile is an initial file that can be used by both the Bourne shell and Bash. From the perspective of separate use of various initial files, it is more appropriate to set a Bash-specific. bash_profile.

Related Article

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.