Shell First: BASH Environment

Source: Internet
Author: User
Tags aliases builtin clear screen egrep

What is a shell?

The shell typically represents two levels of meaning, one is the command interpreter, such as bash, and the other is the shell script. In this section we stand on the command interpreter's point of view to illustrate the shell

Command interpreter shell development history, Sh-csh-ksh-tcsh-bash, let's start with the command interpreter BASH.

Priority of TWO commands

The commands are divided into:

==> Alias
==> Compound Commands
==> function
==> build_in
==> Hash
==> $PATH
==> Error:command not found

Get a command according to the above priority to find, first find the same name alias command, and then find the compound command ...

=================================part1

Let's start with the simplest: aliases, internal commands, external commands, to explore the priorities of the three of them.

alias : Alias command to simplify the integration of output to a long parameter command, alias definition method alias la= ' Ls-al ' Alias Unalias la
internal command : The command that comes with bash is simple, the help of internal commands in builtin (1)
Span style= "COLOR: #ff0000" > external command : is a small program exists in/bin//sbin//usr/bin and other places



[Email protected] ~]#
[[email protected] ~]# alias CD
-bash:alias:cd:not found
CD is an internal command belonging to the Bash software comes with commands (refer to man CD) it does not define aliases

[Email protected] ~]#
[[email protected] ~]# alias ls
LS is defined as an alias
Alias ls= ' ls--color=tty '
[Email protected] ~]#
[[email protected] ~]# which LS
Alias ls= ' ls--color=tty '
/bin/ls
[Email protected] ~]#
LS is actually an external command that belongs to an executable program that is compiled from C code.
[Email protected] ~]# File/bin/ls
/bin/ls:elf 32-bit LSB executable, Intel 80386, version 1 (SYSV), for Gnu/linux 2.6.9, dynamically linked (uses shared Li BS), for Gnu/linux 2.6.9, stripped
[Email protected] ~]#


Authentication: Aliases are precedence over internal commands and external commands

[[email protected] ~]# alias cd= ' ls-l ' #建立一个别名是cd但实际指向的是/bin/ls aliases
[Email protected] ~]# CP/BIN/HOSTNAME/USR/LOCAL/SBIN/CD #在PATH第一个目录里放入一个cd命令
You have new mail in/var/spool/mail/root


[[Email protected] ~]# CD #此时执行cd命令 is found alias CD first
Total 900
-rw-r--r--. 1 root root 0 Mar 10:10 4.txt
-RW-------. 1 root root 956 Mar 8 09:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 10:09 a.txt
-rw-r--r--. 1 root root 0 Mar 10:10 D.txt
Drwxr-xr-x. 9 Jack Jack 4096 Mar 14:40 nginx-1.10.3
-rw-r--r--. 1 root root 911509 Mar 12:49 nginx-1.10.3.tar.gz
[[email protected] ~]# Unalias CD #删除了别名, at this time to search is the internal command first get the Real CD command
[Email protected] ~]# CD #执行的就是系统内置的cd

[Email protected] ~]#/USR/LOCAL/SBIN/CD #此时想越过内部命令去执行外部命令 is the CD that was left before CP/BIN/HOSTNAME/BIN/CD
Miwifi-r3-srv

[Email protected] ~]# RM-RF/USR/LOCAL/SBIN/CD
[Email protected] ~]#

Summary One : command execution search Order
==> Aliases (alias can be viewed)
==> Bash Internal commands
Search ==> $PATH in each path separated by a colon

PS: Where does the pre-login alias come from (aliases should learn something: alias and alias, Cure alias configuration We'll cover it in a later section, but define alias in a file):

[[email protected] ~]# alias
Alias cp= ' Cp-i '
Alias egrep= ' Egrep--color=auto '
Alias fgrep= ' Fgrep--color=auto '
Alias grep= ' grep--color=auto '
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 '
Alias Which= ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde '

[Email protected] ~]#

Some of the. BASHRC from/ROOT/.BASHRC Ordinary users do not contain aliases, because the. BASHRC template for the normal user is:/ETC/SKEL/.BASHRC
and root this. BASHRC is pre-installed when the system is finished.
[[email protected] ~]# grep ' ^alias '/ROOT/.BASHRC
Alias rm= ' Rm-i '
Alias cp= ' Cp-i '
Alias mv= ' Mv-i '
[Email protected] ~]#
Some executable files from the/etc/profile.d/directory

[[email protected] ~]# grep-rn ' Alias '/etc/profile.d/
/etc/profile.d/colorgrep.csh:9:alias grep ' grep--color=auto '
/etc/profile.d/colorgrep.csh:10:alias egrep ' Egrep--color=auto '
/etc/profile.d/colorgrep.csh:11:alias fgrep ' Fgrep--color=auto '
/etc/profile.d/colorgrep.sh:5:alias grep= ' grep--color=auto ' 2>/dev/null
/etc/profile.d/colorgrep.sh:6:alias egrep= ' egrep--color=auto ' 2>/dev/null
/etc/profile.d/colorgrep.sh:7:alias fgrep= ' fgrep--color=auto ' 2>/dev/null
/etc/profile.d/which2.csh:5:# alias which ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde '
/etc/profile.d/which2.sh:4:alias Which= ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde '


=================================part2

Built-in Commands bash built-in commands
Many kinds of
First: Bash's own features see man CD
NAME
Bash,:,., [, alias, BG, bind, break, Builtin, CD, command, Compgen, complete,
Continue, declare, dirs, Disown, Echo, enable, Eval, exec, exit, export, FC, FG,
Getopts, hash, help, history, jobs, kill, let, local, logout, popd, printf,
pushd, PWD, read, ReadOnly, return, set, shift, shopt, source, suspend, test,
Times, trap, type, typeset, Ulimit, Umask, Unalias, unset, wait

The second type: Compound Commands
For example, if while and so on
See Mans Bash

The third type: function
[[Email protected] ~]# function CD () {echo ' my function CD ';}
What functions can be found by the set command
[[Email protected] ~]# set |grep CD

CD ()
Echo ' My function CD '

Validation: Alias,compund Priority

[[email protected] ~]# alias if= ' Ls-l '
[Email protected] ~]#
[Email protected] ~]# if #证明别名比compound的优先级高
Total 900
-rw-r--r--. 1 root root 0 Mar 10:10 4.txt
-RW-------. 1 root root 956 Mar 8 09:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 10:09 a.txt
-rw-r--r--. 1 root root 0 Mar 10:10 D.txt
Drwxr-xr-x. 9 Jack Jack 4096 Mar 14:40 nginx-1.10.3
-rw-r--r--. 1 root root 911509 Mar 12:49 nginx-1.10.3.tar.gz


Validation: Priority of compound and function

[[Email protected] ~]# function for () {echo ' my function for ';}
[[email protected] ~]# for ((i=1;i<3;i++));d o echo $i;d one #执行的仍然是compound命令, proving that the compound priority is higher than the function
1
2

Validation: Priority of compound and function

[[Email protected] ~]# function CD () {echo ' my function CD ';}
[[Email protected] ~]# CD #执行的是自己的函数而不是内置命令cd, proving that the function is higher priority than the built-in command
Total 900
-rw-r--r--. 1 root root 0 Mar 10:10 4.txt
-RW-------. 1 root root 956 Mar 8 09:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 10:09 a.txt
-rw-r--r--. 1 root root 0 Mar 10:10 D.txt
Drwxr-xr-x. 9 Jack Jack 4096 Mar 14:40 nginx-1.10.3
-rw-r--r--. 1 root root 911509 Mar 12:49 nginx-1.10.3.tar.gz


Summary II
==> Alias
==> Compound Commands
==> function
==> build_in

=================================part3
Search for $path when aliases and internal commands are not found

[Email protected] ~]# sed-n ' s/:/\n/gp ' <<< $PATH
/usr/local/sbin
/usr/local/bin
/sbin
/bin
/usr/sbin
/usr/bin
/root/bin


Because there are too many paths in this variable, there are many executables in each path.
It's obviously unwise to search every executable file under each path every time.
To reduce $path search, the content of the last search can be reused for the next execution
Bash creates a hash table for external commands that are searched from $path for caching
This cache is independently owned by the session level. Can not be shared with other endpoints because each user's $path may be different

[Email protected] ~]# hash-r #清除缓存
[Email protected] ~]# hostname #每执行一次就缓存一次
Miwifi-r3-srv
[[email protected] ~]# hash #查看缓存表, found a more cache

Hitscommand

1/bin/hostname

[Email protected] ~]# mv/bin/hostname/sbin/#将hostname命令移动到另外一个目录
[[email protected] ~]# hostname #再次执行hostname, is still read from the cache in the location of hostname, proving that the hash priority is higher than the path, the hash cache is the path path, and built-in commands such as CD and so there is no path, So there is no need for a verbose test, and the built-in priority must be higher than the hash.
BASH:/bin/hostname:no such file or directory

Summarize the order in which command execution is obtained:
==> Alias
==> Compound Commands
==> function
==> build_in
==> Hash
==> $PATH
==> Error:command not found

Ternary characters

Special characters in Bash, and special characters that can be knocked out on the keyboard, have a special meaning, emphasizing one thing: metacharacters are interpreted by the shell

"Command in exchange for the execution of the order.

[[email protected] ~]# ls
4.txt anaconda-ks.cfg a.txt B.txt c.txt D.txt nginx-1.10.3 nginx-1.10.3.tar.gz
[[email protected] ~]# res= ' ls ' #取命令的运行结果, assign to variable res
[Email protected] ~]# echo $res #查看变量res的值
4.txt anaconda-ks.cfg a.txt B.txt c.txt D.txt nginx-1.10.3 nginx-1.10.3.tar.gz

$ () ibid, but it makes up for the "nesting flaw

[Email protected] ~]# res= ' echo ' ls ' #嵌套使用后无法达到预想的效果: Take the effect of the Echo pile file name.

[Email protected] ~]# echo $res
Ls

[Email protected] ~]# res=$ (echo $ (LS)) #替代方案
[Email protected] ~]# echo $res
4.txt anaconda-ks.cfg a.txt B.txt c.txt D.txt nginx-1.10.3 nginx-1.10.3.tar.gz

~ Home Directory

[Email protected] tmp]# CD ~
[Email protected] ~]# pwd
/root

! Take Non-

[Email protected] ~]# LS/DEV/SDA
SDA SDA1 Sda2
[[email protected] ~]# ls/dev/sda[0123]
/dev/sda1/dev/sda2
[[email protected] ~]# ls/dev/sda[!01]
/dev/sda2

! History command Invocation

[Email protected] ~]#!343
Hostname
Miwifi-r3-srv

! Match last History command

[Email protected] ~]#!ls
LS/DEV/SDA[!01]
/dev/sda2

! LS with a space to reverse the return value of the command

[[email protected] ~]# echo OK
Ok
[[email protected] ~]# echo $? #上一条命令执行的结果, 0 for execution success, not 0 for execution failure
0
[[email protected] ~]#! echo OK #将结果取反
Ok
[[email protected] ~]# echo $?
1

0-255, 0 is true, not 0-bit false

@ No special meaning

# Notes

$ variable Value
$ () Same as '
${} variable name range

$[] integer calculation echo $[2+3]-*/% floating point with echo "scale=3; 10/3 "| Bc-l

[Email protected] ~]# money=10
[Email protected] ~]# echo $money
10
[Email protected] ~]# echo 00000$money
0000010
[Email protected] ~]# echo $money 0000

[Email protected] ~]# echo ${money}0000
100000

% kill background process jobs number; Take the mold

^ Take non-and! Identical

[[email protected] ~]# ls/dev/sda[^01]
/dev/sda2
[[email protected] ~]# ls/dev/sda[!01]
/dev/sda2

^ Replace

[Email protected] ~]# systemctl Restart Network
[Email protected] ~]# ^network^sshd^
Systemctl Restart sshd

& Background Execution;&& logic and

* match any length string; Calculate multiplication

() executes in a child process

[Email protected] ~]# x=1
[Email protected] ~]# (x=666)
[Email protected] ~]# echo $x
1
[Email protected] ~]#
[Email protected] ~]# (X=666;echo $x)
666

-minus; interval; CD-;

_ No special meaning

+ PLUS sign;

= assigned value

| pipelines; | | Logical OR

Escape

{} Command list, note that the beginning and end of parentheses must be a space   {ls; CD/; }

[] character wildcard, matching one of the parentheses;

: null Command truth

[Email protected] ~]#:
[[email protected] ~]# echo $?
0

; Multiple commands can be received: Ls;pwd;echo 123; Whether right or wrong, will continue to the last command

"Soft-cited" hard-cited

< input redirection

> Output redirection

>> Append

<< here Document

>& merging 2 and 1 outputs

, enumerate separators

. source; Current directory

/directory Separators

? Single character

Enter command execution

* Wildcard characters: any character
Wildcard characters: any character
One of the [ABC] list items
[^ABC] can also use the range [A-z] for the list to represent the aabbcc...,[0-9] for the 012345 ...
{} Loop List

[[email protected] test]# Touch {1..3}{a ... D}.txt
[[email protected] test]# ls
1a.txt 1b.txt 1c.txt 1d.txt 2a.txt 2b.txt 2c.txt 2d.txt 3a.txt 3b.txt 3c.txt 3d.txt

Range of control variable names echo ${ab}c

Hard references and soft references

[Email protected] test]# x=1
[[email protected] test]# echo "$x" #双引号的代表软引用, special characters in quotation marks have special meanings, such as $, ' etc.
1
[[email protected] test]# echo ' $x ' #单引号代表硬引用, all characters in quotes have no special meaning
$x

\ Turn Meaning

[[email protected] test]# echo \ \
\
[[email protected] test]# echo \ '

[[email protected] test]# echo "'"

Four Bash properties

BASH SHELL Properties
Some of the parameters of their own properties are stored in bash, enabling or turning off a feature
For example, control *. Whether the character is a wildcard
View parameter Set-o
Close the Noglob parameter
# Set-o Noglob
# ls *
LS: *: no file or directory
# set +o Noglob
LS *

Curing settings
The change variables properties we learned earlier, and so on, are restored to the default values after the machine restarts in memory.
So how do you cure these settings to make them permanent?
This requires an understanding of bash two types
1. Login Shell 2. Non-login shell
Login Shell
is by entering the user name password or Su-the shell obtained by
The non-login shell is a shell environment that is opened with bash commands and scripts
So what's the difference between them? What does it have to do with our curing setup?
We know that everything is a file in Linux, and the Shell's attribute loading is also written to the file.
The contents of the corresponding file are loaded at login to initialize the shell environment.
The difference between a non-login and a login is that the loaded file is different, resulting in a different shell environment
Let's see if the login shell loads those files.
-/etc/profile
-/etc/profile.d/*.sh
-$HOME/.bash_profile
-$HOME/.BASHRC
-/ETC/BASHRC
Then look at the files loaded by the non-login shell
-$HOME/.BASHRC
-/ETC/BASHRC
-/etc/profile.d/*.sh
Visible, the non-login shell loads a lot less files
Then we want to cure a configuration when the login is in effect, it is obvious to know which file to write.
In general, we will set environment variables in the $HOME/.bash_profile
If the variable you want to use regardless of the login is set in the $HOME/.BASHRC

Command Completion TAB key
Simplified input tips to prevent writing errors


Historical records
Up and down key check
History Query with!id call
Ctrl+r input Match

Shortcut keys
CTRL + A header
Ctrl+e End of Line
Ctrl+u Delete from cursor to beginning string
Ctrl+k Delete from cursor to line end string
Ctrl+l Clear Screen

Shell First: BASH Environment

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.