Eighth Linux Shell Basics

Source: Internet
Author: User
Tags aliases lowercase

8.1 Shell Introduction


The Linux shell is simply a process of a command line and user interaction. You make an order and it will result. Similar to the CMD window inside windows.

8.2 Shell Command History

All the commands that have been knocked out exist <ls/root/.bash_history>, this file can save up to 1000 records
<echo $HISTORY > View the maximum number of environment variables that can be saved


The command you just tapped is stored when you exit the terminal.
<vi/etc/profile> Modify the value of histsize, to make the newly modified value effective, <source/etc/profile> is required to take effect.
or exit the command line and enter it in effect.

<chattr +a ~/.bash_history> Set hidden permissions can only append cannot be deleted, all commands running will be saved.
The command line does not exit properly, and the saved command is not complete.
<!! > Execute Last Command
<!n> N is a number, the first few commands
<!echo> re-order history in reverse order to find the first echo Start command and execute.

8.3 tab command completion and aliases

<yum install-y bash-completion> Installation Parameters complete package, need to restart the system will not take effect.
<alias restartnet= ' systemctl restart Network.service ' > alias, in memory, expired after reboot


Aliases are stored under ~/.BASHRC and/etc/profile.d/
<vim ~/.bashrc>

<cd/etc/profile.d/> access to the alias storage area

8.4 Wildcard Characters

<ls *txt> Pass All characters
<ls?. Txt>? Represents an arbitrary character
<ls [0-5].txt> lists the characters in 0-5
<ls [23].txt> 2 or 3 of the pass
<ls [0-9a-z]> 0-9,a-z
<ls {1,2,3}.txt>1,2,3 or a wildcard

8.5 Redirection of input and output
Output redirection:
<cat 1.txt>2.txt> Output The former to the latter, equivalent to rewriting
<cat 1.txt>>2.txt > Append the former to the latter, equivalent to the additional
<ls aaa.txt 2>err > output error messages to Aaa.txt
<ls aaa.txt 2>>err > appending error information to Aaa.txt is equivalent to appending
<ls [12].txt aaa.txt &>a.txt>> aaa.txt not present, 1.txt,2.txt exists, will output correct and error messages to A.txt
<ls [12].txt aaa.txt >1.txt 2>a.txt>> output correct information to 1.txt, error message output to A.txt
Enter redirection:
<wc-l<1.txt> input 1.txt information into the WC directive. The left must be a command

8.6 Pipe symbol and job control

Pipe break: Outputs the contents of the former to the back.
<ls |wc-l > Statistics ls How many files are listed
<find./-type F |wc-l> count all files
<vmstat 1> a continuously updated program
Ctrl +z pause the current task to move it to the background
FG pauses the current task and places the task in the foreground
BG moves the current task to the background
All of Jobs's current tasks

Sleep 200 Sleep 200s

Sleep 4000 & Create a dormant task and throw it backstage

8.7 shell variable (top)
8.8 Shell variable (bottom)


env command to view the system variables
Set many variables, and contain user-defined variables;

Single quote ' ' in the case of justification, with a special symbol with single quote '

Multiple variables are superimposed on each other and output with multiple double quotes "" to connect together;
Why does the a= "A$BC" output a?, only a is actually recognized, the back is not recognized

W View all logged-in user information
echo $SSH _tty View the terminal TTY currently owned;

In fact, the variables defined by the polygon are only active at the current terminal under bash.

Pstree View the current shell path;
Global variables defined through export are valid downward and must be under the same sshd.
Exit back to the upper-level shell path;
Bash creates a layer of shell
Sshd landed a terminal to create an sshd;

Unset a cancels the definition of a variable whose name is

8.9 environment variable configuration file


/etc/is the environment variable of the system
~/under the user's own home directory environment variables
PS1 defined in/ETC/BASHRC < vim/etc/bashrc>
PS1 instruction in PPT with color display


8.10 Shell Special Character _cut command
8.11 Sort_wc_uniq Command
8.12 Tee_tr_spit Life

< cat/etc/passwd |head-2 |cut-d ":"-F 1-5 > take the first two lines with: 1-5 segments divided
< cat/etc/passwd |head-2 |cut-c 4 > take 4th character


<sort/etc/passwd> sort display, default by ASCII

<sort-n 2.txt> are sorted by number size, and characters and symbols are all 0
<wc-l 2.txt> Count rows
<wc-m 2.txt> count characters, the file default actually each line has a $ symbol, this do not miss out.
<wc-w 2.txt> statistical words, with special characters such as spaces, commas, are counted.
<uniq 2.txt>, remove the same symbol adjacent to it. So first sort on the go-heavy.

The sort uniq Head cat command only does some work on the file and outputs it, and does not change the contents of the file;

"Tee" is equivalent to redirect Directive ' > ', but will be output on the screen
< >a.txt > Empty the contents of a file A.txt
<sort 2.txt |uniq-c > To reorder the contents of the 2.txt file.
<sort 2.txt |uniq-c > a.txt> redirect 2.txt content to output, but not print on screen
<sort 2.txt |uniq-c |tee a.txt> redirect 2.txt content to output and print on screen
<sort 2.txt |uniq-c |tee-a a.txt> appends the 2.txt content and prints the appended content on the screen

"TR" Replacement character
< echo "Aminglinux" |tr ' [al] ' [al] ' > lowercase A and L into uppercase A and l [] represent optional
< echo "Aminglinux" |tr ' a ' a ' > turn lowercase a into uppercase a
< echo "Aminglinux" |tr ' [A-z] ' [A-z] ' > all letters to uppercase
<echo "Aminglinux" |tr ' [A-z] ' 1 ' > All letters changed to 1

<split> Cutting
<split-b 100M a.txt>
<find/etc/-type f-name "*conf"-exec Cat {} >> b.txt \;> list all/etc/files under Conf and append to B.txt

<split-b 10000 a.txt> default is byte

<split-l a.txt> cut into 1000 rows

8.13 Shell special characters (bottom)


Multiple commands write one line

正确重定向

> Chasing heavier orientation
2> Error Redirection
2>> Error Append redirect
&> Error and correct redirection
|| Or if the previous command executes successfully, the following command is no longer executed
&& and previous execution succeed to execute subsequent commands
[-D AMINGLINGX] | | mkdir Aminglinux directory will no longer be created if directory exists

Eighth Linux Shell Basics

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.