Bash usage summary, bash usage
In Linux, shell is basically bash by default. Below are some tips I have summarized.
& Background running programs
() Use a sub-shell, such as (cd.../../commlib/; make)
$ () Command replacement, same''
<(Command) output the command to a temporary file
<HereDoc
Example:
For example, you need to use the awk script in the shell script.
Awk-f <(cat <EOF
/Abc /{
Print $0;
}
EOF
)
$ () Perform integer calculation $(66/2 ))
If (arithmetic operation ))
If [[String operation]
Alias defines command alias
Run the dot. Or source command to execute the script in the current shell.
Exec can redirect the file descriptor of the current shell or run another program.
Trap can capture Signals
Nohup prevents issues caused by ssh Suspension
Screen can be used to maintain sessions and is not affected by ssh shutdown.
Export the variable to the sub-shell using export
Tee can distribute the output.
ENV_VAR = VALUE your_program this way you can modify its environment variables for this program. The environment variables of the external shell are not changed.
Tac Inverted File
Directory jump
Cd-quickly return to the previous path
Cd back to the user's home directory
Pushd, popd, and dirs for multi-directory navigation
The pushd command is used to change your current directory and store it in the stack. The popd command is used to remove a directory from the top of the stack and return you to this location. Dirs command to display the current directory stack. (Dir-v-p)
Pushd + n; popd + n can operate virtual directory stacks
Quickly jump to common Directories
You may already know that the $ PATH variable can list bash's "Search PATH". When the requested file cannot be found in the current directory, bash will automatically search for the directory. However, bash also supports the $ CDPATH variable. When trying to change the directory, this variable lists the directory to which the cd command is switched. To use this feature, we need to assign a directory list to the $ CDPATH variable, as shown in the following example:
Bash> CDPATH = '.:~ :/Usr/local/apache/htdocs:/disk1/backups'
Bash> export CDPATH
Now, whenever you use the cd command, bash checks all directories in the $ CDPATH list to find the directory name to be switched.
Special Parameters
1) $ *: represents all parameters. The interval is the first character of the parameter set in IFS.
2) $ @: similar to * asterisks. The difference lies in the absence of reference to IFS
3) $ #: number of parameters
4) $? : Execution of the return value of the previous command
5) $-: Option parameter of the recently executed foreground pipeline
6) $: Process ID
7) $! : Execute the PID of the previous background command
8) $ _: displays the last executed command.
Bash shortcut
Emacs Style
Ctrl + p: direction key Upper Limit
Ctrl + n: direction keys
Ctrl + B: direction keys
Alt + f: move the cursor to the right of a word
Ctrl + f: direction key →
Alt + B: move the cursor to the left
Ctrl + a: move the cursor to the beginning of the line
Ctrl + e: move the cursor to the end of the row
Ctrl + k: Clear the content from the cursor to the end of the row.
Ctrl + d: Delete the letter where the cursor is located. Note the difference with backspace and ctrl + h. The two are the characters before the cursor is deleted.
Ctrl + r: Search for commands that have been used before. There will be a prompt to search for bash history based on the keyword you entered
Ctrl + m: Enter
Ctrl + I: Enter the tab
Ctrl + [: Enter esc
Others
Ctrl + h: Delete the character before the cursor, which is the same as the backspace key.
Alt + p non-incremental reverse search history
Alt +> the last command line in the History command list starts forward
Ctrl + u: Clear all content from the front of the cursor to the beginning of the line.
Ctrl + w: Remove a word before the cursor
Ctrl + t: two characters before the cursor position
Ctrl + y: paste or restore the last Deletion
Ctrl + l: clear screen, equivalent to clear.
Ctrl + xx jump the cursor at the beginning and end of the line
Alt + r undo all contents of the current row
Ctrl + z: run the current process in the background.
Ctrl + s: Lock the screen
Ctrl + q: Restore the screen
Ctrl + v key: enter special characters
Alt + l convert the letters after the current cursor to lowercase letters
Alt + u convert the letters after the current cursor into uppercase letters
Ctrl + Alt + e extend the contents of the command line (for example, ls => ls-l -- color = tty)
Ctrl + c: kills the current process. In input mode, the input command is interrupted.
Ctrl + d: exit the Current Shell
Esc +. The shortcut key can poll the parameters or options of History commands.
The esc + t shortcut can replace the first two words.
Enter the repeated letter Esc {100} e, which can contain 100 characters.
{Esc} can be supplemented multiple times
{Esc }{~} The user name on the machine can be supplemented.
{Esc} {/} can complete the file name
{Esc} {@} can complete the host name, And localhost can be easily completed with lo.
Bang History commands
!! Run the previous command again.
! N re-execute the nth command. For example! 3
! -N: Re-execute the nth command .! -3
! String re-execute the command with the string header. For example! Vim
!? String? Re-execute the command that contains the string. For example !? Test. cpp?
!? String? % Replace with: the parameter of the command that recently contains this string. For example: vim !? Test. cpp? %
! $ Is replaced with the last parameter of the previous command. For example, vim! $
!! String is appended to the end of the previous command and executed.
! Nstring appends string after the N command and executes it.
^ Old ^ new ^ replace the previous command
Modify
: S/old/new/replace the new that appears for the first time in the N-th command with old. For example, vim !? Test. cpp? : S/cpp/c/
: Gs/old/new/replace all
: Wn w is a number, and the w parameter of the command is used.
: P echo command instead of execution, such! Ls: p. This is very useful. You can check whether the command you selected is correct and use it again when you want to execute it !!
Bash files
/Etc/profile set environment variables (All Users)
~ /. Bash_profile set the environment variable (current user)
~ /. Bashrc
~ /. Bash_history
~ /. Bash_logout