Bash Basics of Shell

The scripting language that Shell interprets and executes can directly call all commands of Linux.

 

echo output command

-e: supports backslash-controlled character conversion



 

eg1:echo -e 'ab\bc'    

delete left character

 

eg2:echo -e 'a\tb\tc\nd\te\tf'

Tab and newline output

eg3:echo $PATH

View the environment variables under linux

 

history command

Syntax: history [options] [history command save file]

Options

-c clear history command

-w Write the history commands in the cache to the history command save file

~/.bash_history default save address

 

By default, 1000 historical commands are saved, which can be modified in the environment configuration file /etc/profile.

 

History command calls

a, use the up and down arrows to recall previous history commands

b, use "!n" to repeatedly execute the nth history command

c, use "!!" to repeat the previous command

d, use "! string" to repeat the command at the beginning of the string

 

Command and file completion

Pressing the "Tab" key will automatically complete it.

 

alias alias command

Syntax: alias alias='original command'

View a list of aliases: alias

vi ~/.bashrc makes aliases permanent

Remove aliases: unalias aliases   

 


 

Common shortcut keys in Linux

The blue ones are commonly used and need to be skilled.

 

Note: Letters are not big or small

 

I/O redirection

output redirection

> output console information to the specified file

>>



 


eg1:df -h > log.txt #correct output redirection, overwrite

eg2:lst 2>> error.txt #Error output redirection, append

eg3: ls &> log.txt #correct or error output redirection, overwrite

eg3: lst &>> log.txt #correct or incorrect output redirection, append 

 

input redirection

< takes a file as input to the command.

eg1:wc -l < ​​log.txt #Enter the contents of log.txt to the command before <.

eg2:wall <vi.txt send vi.txt message to everyone via redirection

 

<<

Take the command between two symbols as input

eg: wc << hello

aaa

bbb

ccc

hello

Note: The symbol (hello) in the last line will end when the symbol (hello) after the first group of "<<" is the same, and only the content between them will be recognized.

 

 multiple commands

; 、&& 、||



 

eg:lst ; date ; cd /usr/local

eg:lst && date

eg:lst || date

 

Pipe character: |

Syntax: command1 | command2

Correct output of command 1 as the action object of command 2

Eg: netstat -an | grep 'ESTABLISHED'

 

netstat network connection command

Syntax: netstat [options] 

eg1:netstat -an | grep 'ESTABLISHED' to see how many connections the current computer has

eg2:netstat -antp | grep nginx To view the program whose program name contains "nginx" in the current computer connection, the occupied port, occupied process number, and program name will be displayed.

 

grep searches files for matching strings

Syntax: grep [options] "search content"

Options

-i ignore case

-n output line number

-v reverse lookup

--color=auto Display the searched keywords in color.

eg: grep -n --color=auto "port" repl1.conf Search for the color keyword in the repl1.conf file, and display the line number and display it in color

 

 

wildcard

 

Eg:

Prepare the data to create the following files

touch abc

touch abcd

touch 0abc

touch 5abc

touch aabc

touch fabc

touch 123

 

ls ?abc

ls *abc

ls *abc*

ls [abcdefg]abc

ls [0-9]abc

ls [^0-9]abc

 

other special symbols

 

Eg:

name=sc

echo '$name'

echo "$name"

echo "\$name"

 

abc=`data`

echo $abc

 

abc=$(date)

echo $abc

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326166481&siteId=291194637