Linux martial arts cheats (3) shell learning - Bash shell


Powerful features of the Bash shell

/bin/bash is the default shell for Linux, and Bash is also the standard shell for Linux distributions.
Bash is an enhanced shell version formed by adding some user requirements on the basis of sh.
Why is Bash the default shell for Linux? This is to introduce several user-friendly functions of Bash, which are simply too easy to use.

Save command history (history)

1. As long as you press the "up and down keys" in the command line, you can find the commands entered before/after, and the memory capacity of this command can reach 1000.
2. The commands are stored in ~/.bash_history, but note that the commands stored here are the commands executed before login; the commands executed by the

  current login will be temporarily stored in the memory (use the history command to view), and the history command When I log out, the recently executed command will be recorded in my log file, and when I log in to the system, the command memory will be recorded in .bash_history. Of course, you can also use history -w to force immediate writing.
This function allows the user to view every command that has been operated before.

history [n]
history [-c]
history [-raw] histfiles
n number, meaning "to list the most recent n commands"
-c Delete all history content in the current shell
-a Add the newly added history command to histfiles, if no histfiles is added, it will be written to ~/.bash_history by default
-r Read the contents of histfiles into the history memory of the current shell
-w Write the current history memory content into histfiles

List all the history memory in the current memory: history
List the latest 3 data: history 3
Immediately write the current data into the histfile: history -w (by default, the history will be written to ~/ .bash_history)
view ~/.bash_history will record several data: echo ${HISTSIZE}

!number
!command
!!
number How many instructions to execute
command Search forward from the most recent command for the command whose "command string begins with command" and execute it
!! Execute the previous command (equivalent to pressing the ↑ button and then pressing Enter)
These three commands are also quite easy to use, good brothers who are interested can try it! !

Command and file completion key [Tab]

There is this function in bash that can save you from typing a lot of words and the data entered is correct.
The [Tab] button is connected after the first word and the second word of the command, and the completion content will be different.

When [Tab] is connected after the first word of a series of instructions, it is command completion;
when [Tab] is connected after the second word of a series of instructions, it is "file completion"!

If you want to print out all the commands beginning with c in the current environment, press " c[tab][tab] ".


Command alias setting (alias, unalias)

If some commands you are used to execute are too long, you can replace these commands with aliases, which is easy to execute.
For example, if you want to view all types of files and file attributes under the file, you need to use the "ls -la" command to view. This command is not concise enough, you can use "la" to replace this command. In the future, just typing "la" is equivalent to typing "ls -la"

Commands using aliases: add {"alias"='command options...'}
after alias: alias la='ls -la'

If you delete some files as root, execute "rm filename" directly without any prompt information. In case you accidentally type the wrong file name, you will wake up when the command is executed. At this time, you can use "rm -i" as an alias for the "rm" command. This will save you a lot of worry when executing the "rm" command. alias rm='rm -i'

View all aliased commands in the current system

DWB@ubuntu:~$ alias

alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias rm='rm -i'

If you want to cancel the command alias, then use unalias. For example, to remove the alias of the rm command, use unalias rm


job control, foreground, background

Job control (jobs) allows us to throw work into the background at any time. Don't be afraid to accidentally use [Ctrl] + c to stop the program.


Programmatic scripts (shell scripts)

The shellscripts under Linux play a more powerful function. You can write the frequently used continuous commands in a file, and carry out the detection of the host through the interactive way of conversation. It can also be designed by the environment variables and related instructions provided by the shell.


Wildcard

Want to know how many files starting with C are under /usr/bin?
Use: " ls -l /usr/bin/C* " will be able to know. Speed ​​up the user's operation.


Check whether the command is a built-in command of the Bash shell

How to check whether a command is a built-in command of Bash shell? Use man to view bash related documentation? The command man bash
has a lot of detailed data in the bash man page. It is too troublesome to find out whether a command is a built-in command of the Bash shell. Is there an easier way? hey-hey! ! ! Yes, the type command

type [-tpa] name
When no options and parameters are added, type will show whether name is an external command or a bash built-in command
-t When the -t parameter is added, type will display the meaning of name in the following words:
file: indicates that it is an external command;
alias: indicates that the command is the name set by the command alias;
builtin: indicates that the command is built-in by bash command function;
-p If the following name is an external command, the full file name will be displayed
-a In the path defined by the PATH variable, all commands containing name will be listed, including alias
DWB@ubuntu:~$ type cd                       // cd命令属于内置命令
cd is a shell builtin
DWB@ubuntu:~$ type -t cd
builtin
DWB@ubuntu:~$ type -a cd
cd is a shell builtin
DWB@ubuntu:~$ type ls						// ls命令属于外部命令
ls is aliased to `ls --color=auto'
DWB@ubuntu:~$ type -t ls
alias
DWB@ubuntu:~$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls
DWB@ubuntu:~$ type vi						// vi命令属于可执行文件
vi is hashed (/usr/bin/vi)
DWB@ubuntu:~$ type -t vi
file
DWB@ubuntu:~$ type -a vi
vi is /usr/bin/vi

Note: When using type to search for the following name, if the following name cannot be found in the state of an executable file, then the name will not be displayed. That is, type is primarily looking for "executable" rather than general filenames

Guess you like

Origin blog.csdn.net/weixin_43564241/article/details/129911436