Summary of features of bash in shell

Source: Internet
Author: User
Tags aliases clear screen closing tag

Shell:

An interface program that completes interactive operations between the user and the operating system, providing users with simplified operations; In the 70 's mid-century in Bell Labs, Bourne bit UNIX developed a shell program Bourne Shell, referred to as SH;
Bill Joy: Rewrite c Shell in C according to SH, abbreviated as CSH; lie on the bed for its derivative version;
David Korn: A collection of the advantages of Cshhesh development of the Korn Shell, referred to as Ksh;
Gnu:gnu Organization found SH is more useful and further development of borne Again shell, referred to as Bash;linux system default shell program;
Zsh: Final shell, very powerful; (but not replaced in Linux)
All secure shells in the current system that can be supported:
/etc/shells
Attention:
1. In addition to/sbin/nologin, other shells can be used for testing;
2. When changing the shell, it is best to replace the other shell in bash, if you have replaced another shell, use the exit command to return to bash, and then switch;
3. Select Shell
[Email protected] ~]# Cat/etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[Email protected] ~]# Echo
Bash
[Email protected] ~]# tcsh
[Email protected] ~]# Echo
Tcsh
Switch to the original shell
[[Email protected] ~]# exit
Exit
[Email protected] ~]# Echo
Bash

Features of bash:

One, the alias of the command-(custom command)
alias-defining or displaying aliases
Format: Alias [-P] [Name [= value] ...]
If the alias command does not have any options or parameters, it shows all the alias settings that have been defined and are in effect;
#alias alias= ' COMMAND [OPTION] ... [ARGUMENT] ... '
Unalias
unalias-remove each "name" from the alias definition list
Format: Unalias [-A] name [name ...]
Note: The functions of the alias or the Unalias command will take effect immediately; the lifetime of the two commands is only the current shell's lifetime, but log off, which is equivalent to shutting down bash, the alias defined with the alias command, or a command deleted using Unalias, is immediately invalidated ;
For each user, there is a private bash profile, a hidden file in the user's home directory, a file name. BASHRC, whose main function is to save the aliases of the command files
Note: After editing a command alias in this file, it does not take effect immediately in the currently running bash and requires a reload for the configuration to take effect, the following two actions are possible:
1. Restart bash: Login again after logging off;
2. Point to the command defined in the file: Use the source command, or the. command;
source/.
"Source"-executes a command in a file in the current shell.
“ .” -Commands in a file executed in the current shell
Format: source/. File name "parameters"

Second, bash's shortcut keys
C: Represents the CTRL key
M:alt Key
E:esc Key
Del:backspace Key
C-l: Clear screen, equivalent to clear command;
C-A: Jumps the cursor to the beginning of the command line edit mode
C-E: Jumps the cursor to the end of the line in the command line edit mode
C-K: The current cursor position is deleted to the end of the line
C-u: The current cursor position is deleted to the beginning of the line
C-C: End foreground process
M.: references the last parameter in the previous command; (not available in Xshell)
E Press and hold again.: function with the previous M-. can be used in Xshell

Iii. Command history:
The bash process saves commands that have been executed by the user in its session to make it easier for the user to repeatedly execute a command, and the command history consists of two aspects:
1. Commands that have been executed by the user;
2. The path of the command executed by the user layer;
In bash, there is a built-in variable with the value of the-path,path variable, which is the set of paths for all the commands in the current system.
How is the command executed by the shell?
Bash slices the contents of the command line according to whitespace characters, analyzing the contents of the first slice;
1. Determine whether the content of the first slice is an internal or external command;
If it is an internal command, run directly;
If it is an external command, find the path cache in the hash table first, if any, then execute it directly; otherwise, you will need to find the path variable that contains the file with the corresponding name;
Use the hash command to view the path of the remembered command file;
-R: Empty hash Table
Use the history command to display the command parameters that have been executed;
hietory-Display and Manipulation History list
Format: History [-c] [-D offset] [n] or
HISTORY-ANRW [filename] or history-ps parameter [parameters ...]
Common options:
-c: Clears command history from the history buffer;
-D OFFSET: Deletes the specified history command entry;
Repeat the command for the history buffer:
“! Number ": Call this number command directly in the history buffer;
“! String ": Repeats the last command executed in the history buffer beginning with string;
!? String: Repeats the last executed command that contains "STRING" in the history buffer;
! -Number: Call this number command directly in the history buffer;
Use the up and down arrows to call the history buffer command to enter execution;
C-r: Implement search in the historical buffer and execute after carriage return;
! $: Call the last parameter of the last command in the history buffer;
! ^: A parameter that invokes the first position of the last command in the history buffer;
! : Number: The number position parameter in the last command is called in the history buffer;
! Sequence:number: A parameter in the history buffer that invokes the position of the Numbe in the "seqence" command;
Built-in variables related to commands in bash: (variable name case sensitivity cannot be mistaken)
Histsize: The maximum number of entries in the history buffer that can be saved in a command history: default is 1000;
Maximum number of entries in the Hisifilesize:~/.bash_history file that can be saved in the command history: default is 1000;
Histimefomat: When a History command is recorded in the command history buffer, the timestamp token of the execution of the command is also logged: You can use format of the date command to represent the record format of the time: The default value is empty;
Histcontrol: The way to control the history of commands;
Ignoredups: The command that ignores successive tangent peers is logged to the history buffer, which is the default value;
Ignotrspace: Commands that begin with whitespace characters are not recorded in the history buffer;
Ignoreboth: Neither of the above is recorded in the historical buffer;

Iv. Command Completion-(Tab key use)
Includes two dough content:
1. The completion of the command string;
Refer to the path in the path variable to complete the command;
If the given string is unique in the path corresponding to the paths variable, the command is directly complete;
If the given string is not unique within the path of the path variable, bash gives a hint;
2. String completion for parameters in the command
Complete the completion according to the given path:
If the given string is unique in the given path, the command is directly complete;
If the given string is not unique in the given path, Bash gives a hint;

V. Command line expansion
1.~ Expand:
~:bash will automatically expand it as the home directory of the currently logged-in user;
~string:bash will automatically expand it to the user's home directory with "STRING" as the user name;
~+: Call Bash's built-in variable "PWD" value;
~-: Call the value of Bash's built-in variable "oldpwd";
2.{} Expand:
In {}, you can populate a list of paths separated by "," (in the English input method, ","), and bash expands it into multiple independent paths;
Two types of applications
[Email protected] ~]# MKDIR-PV/CHINA/{HB,HN,QD,BJ}/{1,2,3}/CLASS18
mkdir: The directory "/china" has been created
mkdir: The directory "/CHINA/HB" has been created
mkdir: The directory "/CHINA/HB/1" has been created
mkdir: The directory "/CHINA/HB/1/CLASS18" has been created
mkdir: The directory "/CHINA/HB/2" has been created
mkdir: The directory "/CHINA/HB/2/CLASS18" has been created
mkdir: The directory "/CHINA/HB/3" has been created
mkdir: The directory "/CHINA/HB/3/CLASS18" has been created
mkdir: The directory "/china/hn" has been created
mkdir: The directory "/CHINA/HN/1" has been created
mkdir: The directory "/CHINA/HN/1/CLASS18" has been created
mkdir: The directory "/CHINA/HN/2" has been created
mkdir: The directory "/CHINA/HN/2/CLASS18" has been created
mkdir: The directory "/CHINA/HN/3" has been created
mkdir: The directory "/CHINA/HN/3/CLASS18" has been created
mkdir: The directory "/CHINA/QD" has been created
mkdir: The directory "/CHINA/QD/1" has been created
mkdir: The directory "/CHINA/QD/1/CLASS18" has been created
mkdir: The directory "/CHINA/QD/2" has been created
mkdir: The directory "/CHINA/QD/2/CLASS18" has been created
mkdir: The directory "/CHINA/QD/3" has been created
mkdir: The directory "/CHINA/QD/3/CLASS18" has been created
mkdir: The directory "/CHINA/BJ" has been created
mkdir: The directory "/CHINA/BJ/1" has been created
mkdir: The directory "/CHINA/BJ/1/CLASS18" has been created
mkdir: The directory "/CHINA/BJ/2" has been created
mkdir: The directory "/CHINA/BJ/2/CLASS18" has been created
mkdir: The directory "/CHINA/BJ/3" has been created
mkdir: The directory "/CHINA/BJ/3/CLASS18" has been created
Frequently used to create multiple catalogs at the same time
[[email protected] ~]$ ls
C.txt download Public Picture document Desktop template Video Music
[[email protected] ~]$ CP C.txt{,.bak}
[[email protected] ~]$ ls
C.txt c.txt.bak Download Public picture document Desktop template Video Music
Vi. execution results of the command
In bash, there are two execution results for commands:
1. Output of the execution command:
The normal output result of command executed according to user's requirement;
Command reference or command substitution:
"(Wavy line key using English Input method) is equivalent to $ ()
": Inverted single quote, inverted quotation mark
Example:
[[email protected] ~]# ls
123 ABC Anaconda-ks.cfg a.txt b.txt initial-setup-ks.cfg public template video picture document download music Desktop
[[email protected] ~]# Touch a.txt-$ (date +%f-%h-%m-%s)
[[email protected] ~]# ls
123 ABC anaconda-ks.cfg a.txt a.txt-2017-10-26-14-40-46 b.txt initial-setup-ks.cfg public template video picture document download music Desktop
[[email protected] ~]# Touch a.txt-' Date +%f-%h-%m-%s '
[[email protected] ~]# ls
123 ABC anaconda-ks.cfg a.txt a.txt-2017-10-26-14-40-46 a.txt-2017-10-26-14-41-48 b.txt initial-setup-ks.cfg Public template video Picture document download music Desktop
This action is often used to mark backup times when a file is backed up
2. Execute the status return value of the command:
Whether the previous command successfully executed the relevant return content;
Bash uses a special built-in variable to save its contents: $?
$? Actually, it's a numeric value, the range of values: 0~255
which
0: Indicates that the command was executed successfully;
1~255: Indicates command execution failed
Failed status return values for commands built into the 1,2,127:bash;
1: Some small problems
2: A serious problem
127: Indicates that the command is capable of problems;
It is associated with the number (3-126,128-255): The user can customize the return value of the failed state;

Seven, reference function:
": Strong references
Whatever is quoted in quotation marks, bash is treated as a normal character, even if it is the character that bash defines as a special function and function, except the single quotation mark itself;
"": Weak reference
In the content quoted by double quotation marks, some special characters still retain their special meanings, such as: $,\, ' "";
For example:
[Email protected] ~]# date +%f +%t
Date: Extra operand "+%t"
Try ' Date--help ' for more information.
[[Email protected] ~]# date + "%F%T"
2017-10-26 15:25:19
[[Email protected] ~]# date + '%F%T '
2017-10-26 15:25:43

Viii. escape function
  \:bash-defined escape character: In most cases, "\" can only be escaped by one character after it;
For example: [[email protected] ~]# echo \ $PATH
$PATH
[[email protected] ~]# echo \$ $PATH
$/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
\ Command: The function of the command itself, which cancels the defined commands alias;

Nine, globbing--file name wildcard character, abbreviated as Glob:
 *: Matches any character or string of any length, ("." In some special places) is not a match)
 ? : matches any single character, ("." In some special places) cannot be matched)
 []: matches any specified range of people in a single character, must match and match only one character;
  Specify a range of methods:
  1. Enumeration method: Lists all valid characters;
  2. Scope designation:
[0-9]: represents all individual decimal digits;
[A-z]:a,a,b,b......y,y,z (does not contain z)
[A-z]:a,b,b,c......y,y,z,z (does not contain a)
  3. Bash built-in character set:
    [: Lower:]: All lowercase letters
    [: Upper:]: All uppercase letters
    [: Alpha:]: All letters include case
    [:d igit:]: All single decimal digits
    [: Alnum:]: All letters and decimal digits
    [:p UNCT:]: All symbols
    [: space:]: denotes whitespace characters, including spaces and tabs
  4. Reverse matching:
    ^
    Example:
  & nbsp   #ls a[^[:d igit:]?? The second character representing the file name cannot be a decimal digit;
      NOTE: When there is a ^ in parentheses indicating a character set inverse match, note the use of *, because the wildcard range may cause a reverse match to fail;
This type of failure is because Bash's globbing is matched in greedy mode;

Ten, Pipeline
|
Pipelines are commonly used to connect multiple commands: The output of the command that precedes the pipe symbol is passed through the pipeline to the command following the pipe symbol, so that the following command uses the preceding output as a parameter;
Wc
Wc-print newline, Word, and byte counts for each file
Format: WC [OPTION] ... [FILE] ...
Common options:
-C,--bytes: Displays only the number of bytes in the file;
-M,--chars: Shows only the number of valid characters in a file
-L,--lines: Displays only the number of lines of content in the file
-W,--words: Displays only the number of words in the file
Attention:
1. Generally speaking, any command that is connected by a pipe should be able to complete the output of the command result after successful execution, otherwise the pipeline is meaningless;
2. Pipeline is not omnipotent, some occasions can not use the pipeline;

Xi. Input and output redirection
In the use of computing, the realization of a function of the main body is a program;
program = instruction + data
Data is critical!
file, io--input and output
In a Linux system, the entity that can complete input and output: file;
Devices that can be used for input: Files
Keyboard devices, general files in the file system, network card devices, sound card devices, etc.
Files that can be used for output: Files
Monitor, file system of regular files, network card devices, sound card devices, disk equipment, etc.;
The way to use a file in memory is to define a file descriptor that corresponds to a file;
0: The input data stream that is input, stdin, from the keyboard;
1: Indicates the output data stream of callout output, stdout, to the monitor;
2: Error data stream indicating standard error, stderr, to the monitor;
Input-output redirection (IO redirection)
IO operations implemented with non-standard device files;
Enter redirection:
< (can be omitted in many cases)
Output redirection:
>: Overwrite output redirection
~]# set-c//Prevent accidental overwrite of source file switch, you can use ">|" The symbol finishes overwriting the redirected output without care about preventing accidental switches from opening
~]# set-c//off Prevent accidental overwrite of source file switch
>>: Append output redirection
Error redirection:
2>: Overwrite error redirection
2>>: Append error redirect
and labeling output and redirection of standard errors:
1.command{>|>>}/path/to/somefile 2>&1
2.&>,&>>
Special device Files:
/dev/null:blackhole,bitbucket (bit barrels)
/dev/zero: Bubble machine, spit 0 machine;
Other special symbols in bash:
<<:here document, which is the closing tag for documents here; (very common)
<<<:here string, where the strings, end tags;

Summary of features of bash in shell

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.