Linux Basics-21

Source: Internet
Author: User
Tags aliases

I. Shell's built-in commands

The shell itself provides some commands, called built-in commands, that have been exposed to some of the shell's built-in commands.

Like what:

We use which to view LS and find it in the/bin/directory. But looking at the history, CD, FC, Jobs, set and other commands, these commands are not found in the location, because these commands inside the shell program, called built-in commands.

You can view all the built-in commands with help

[email protected]:~$ helpGNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)......job_spec [&]                                history [-c] [-d offset] [n] or history >(( expression ))                            if COMMANDS; then COMMANDS; [ elif COMMA>......
Second, alias

Aliases allow the user to customize the command name, allowing the user to combine commands with common parameters or options.

(a) Define aliases:

Alias aliases = value

For example, often check the process, run: PS aux, but the total input aux too troublesome, can only use PS equivalent PS aux effect? Of course:

[email protected]:~$ alias ps=‘ps aux‘[email protected]:~$ psUSER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMANDroot         1  0.0  0.1 225292  9124 ?        Ss   09:01   0:02 /sbin/init splashroot         2  0.0  0.0      0     0 ?        S    09:01   0:00 [kthreadd]......

Another example: Can you replace the name of a command with another name, of course:

[email protected]:~$ alias h=head[email protected]:~$ h -2 /etc/os-release PRETTY_NAME="Deepin 15"NAME="Deepin"

In this way, old Mo has a lot of songs placed in a very remote place, because the Deepin music player always stop automatically, so the old Mo often use mplayer in the terminal to play music. But the problem is that every time I get into this faraway place it's too much trouble ...

[email protected]:~$ cd  

Now with alias you should be able to get a piece of it.

Now old Mo want to listen to songs, as long as input music, immediately transferred to the music warehouse. WOW haha!!

(ii) See which aliases are defined

Simply type alias on the go. Such as:

[email protected]:/etc$ aliasalias ll=‘ls -l‘alias ls=‘ls --color=auto‘alias music=‘cd /media/linux/database/me/music‘
(iii) deleting aliases

To delete an alias with the unalias command:

Observation, the music alias was deleted. A small problem, if the alias and command name conflict, who should be executed first?

See, as a matter of fact, the implementation of LS, the result is entered into the/etc/samba directory. What if the LS definition deletes a file? Dead don't know how to die ...

Iii. Custom Prompts

The prompt is waiting for the user to enter the command section. Such as:

There are two common types of prompts:

    • Basic prompt
    • Second prompt

We found that when the input is not completed, the shell continues to wait for user input after pressing ENTER, and the prompt > is the second prompt.

(i) customizing the prompt with PS1 and PS2

These two environment variables are used to define the prompt. PS1 corresponds to the basic prompt, PS2 corresponds to the second prompt. Now that the prompt is prompting for a command, let's change it:

When we use ps1= ' Please enter command: ' After the prompt is defined, the prompt becomes "Please enter command:". Then set the second prompt to: ps2= ' continue typing: ', we try echo again "Hello input practice, found that the second prompt also changed.

Well, let's play it again:

[email protected]:~$ PS1=‘love deepin: ‘love deepin: ls /bin   etc         initrd.img.old  lib64       mnt   recovery  sbin  tmp  vmlinuzboot  home        lastore         lost+found  opt   root      srv   usr  vmlinuz.olddev   initrd.img  lib             media       proc  run       sys   

We can define the prompt with some escape sequences, which list the commonly used escape sequences:

sequence Description
\d Show Date
\ t Show time
\h Display host Name
\u Show user Name
\w Show path
\w Show the last directory name of the path
! Show history number
$ If it is a super user display #, if it is normal user display $

Now Lao Mo Custom A and CentOS-like prompt, because old mo like his path style, regardless of how long the path, always show the last word. The prompt format is as follows: [[email protected] ~]#

PS1=‘[\[email protected]\h \W]\$ ‘

Note: The prompt has a generic rule for a character: # represents a Superuser, $ represents a normal user, and we should remember it.

Four, the configuration file immediately effective

We often modify the ~/.BASHRC file, this file will be automatically called when the user logs in, but we do not want to quit the system and then log back in, this time can be source, equivalent to execute once, reconfigure environment variables.

[[email protected] test]$ echo "alias ld=‘ls -ld‘" >> ~/.bashrc[[email protected] test]$ source ~/.bashrc[[email protected] test]$ lddrwxr-xr-x 2 linux linux 4096 Jul 21 07:50 .

We modified the ~/.BASHRC file, then source it, and found that the LD command can be executed. The same command as the source function is to enter a point before the script, which is equivalent to source.

[[email protected] test]$ . ~/.bashrc

This method is good and does not need to be logged in again. When we re-modify the configuration file, want to take effect immediately be sure not to forget the source.

V. Bash initialization

Bash shells generally fall into four categories: login shell, non-login shell, interactive shell, non-interactive shell.
When you log in to a Linux, you will see the prompt, which is just the login shell. such as: Login to the virtual terminal, Telnet to a host and so on. The login shell is also an interactive shell.

To differentiate us, we cite some examples:
Log in from the Virtual Console, log on through the network to the remote host, X initialize the shell, these are login shells, are interactive shells. Manually run the Bash-initiated shell, not the login shell, but he is the interactive shell, the child shell in command substitution, the sub-shell used by the parentheses Group command, the child shell used to execute the script is not a login shell, nor is it an interactive shell.

(a) Login shell:

Take the Redhat system as an example:
When we log in to the system, the login shell first gets the file/etc/profile, which initializes some global environment variables and then gets the/etc/profile.d/*.sh matching file. After that, the shell will go to the user's home directory to get the ~/.bash_profile configuration file, then get the ~/.BASHRC profile and finally get the/ETC/BASHRC configuration file.

Note: The contents of/ETC/BASHRC are similar to those in/etc/profile, but most of the/ETC/BASHRC definitions are environment variables that are not logged in to the shell.

(ii) non-login, interactive shell

Starting this shell will automatically get ~/.BASHRC and then get the file/ETC/BASHRC.

(iii) Non-login non-interactive shell

Do not get any configuration files.

Note: Only login shells can get/etc/profile,~/.bash_profile, while ~/.BASHRC and/ETC/BASHRC files can be obtained by all shell (login and non-login). The first two are typically used to configure environment variables because they are initialized only once, and the latter two are used for custom variables such as aliases.

(iv) Introduction of the Boot configuration file
file Shell Type Expected functionality
/etc/profile Login Shell Common, valid environment variables
/etc/profile.d/*.sh Login Shell Package-Specific environment variables
~/.bash_profile Login Shell User-specific environment variables
~/.bashrc All interactive shells User-specific aliases, shell features, and shell options
/etc/bashrc All interactive shells Common aliases, shell features, and shell options
(v) Exit login Shell

~/.bash_logout is called when the user logs off, which is rarely used. Some people use it to clean up ~/.bash_history files, but this is not a good habit, because command history can look at the used commands to improve productivity. and system failures can be viewed from the command history, and what commands are executed to cause a failure.

The above configuration files and initialization steps are for Linux for the Redhat series. For the Deepin system there is no/ETC/BASHRC, which corresponds to the/ECT/BASH.BASHRC. And I also found Deepin ~/.bash\profile This file, there are ~/.profile files, online search: If bash is executed in login mode, read ~/.bash_profile, if it does not exist, then read ~/.bash_ Login, if the first two do not exist, read ~/.profile.

Old Mo think why not unify, the old mo all Dizzy ~ ~

Linux Basics-21

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.