Bash Usage Summary

Source: Internet
Author: User

The shell in Linux is basically bash by default, and here are some tips I've summed up.
& Background Run Program
() Use a child shell, for example (CD.. /.. /commlib/; Make
$ () command substitution, and ' the function is the same
< (command) the output of the command to a temporary file
<< HereDoc

Examples of Use:
For example, you want to use the awk script in a shell script
Awk-f < (cat <<eof
/abc/{
print $;
}
Eof
)


$ (()) to perform an integer calculation $ ((66/2))

if (arithmetic operation))
if [[String operations]]
Alias defines a command alias
Dot. or the 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 prevent SSH hangs from causing problems
Screen can be used to keep the session from being affected by SSH shutdown
Export exported variable to child shell using
Tee can shunt the output
Env_var=value Your_program This can be modified for this program environment variables, external shell environment variables have not been changed
Tac Inverted file

Directory Jump
CD-Fast back to previous path
CD back to user's home directory
pushd, POPD, dirs implement multi-directory jump
The pushd command is used to change your current directory and store it in the stack. The popd command is used to remove the directory from the top of the stack and return you to that location. The dirs command to display the current directory stack. (dir–v–p)
PUSHD +n; POPD +n can manipulate the virtual directory stack

Quickly jump to a common directory
You may already know that the $path variable can list bash's "Search path"-a directory that bash automatically searches for when the requested file cannot be found in the current directory. However, Bash also supports the $cdpath variable, which lists the directory to which the CD command turns when attempting to change the directory. To use this feature, we will assign a list of directories 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 will check all the directories in the $cdpath list to find the directory name to turn to.

Special parameters
1) $*: Represents all parameters, the interval is the first character of the IFS default parameters
2) [email protected]: similar to * asterisk. The difference is that we don't refer to IFS
3) $#: Represents the number of parameters
4) $?: Executes the return value of the previous instruction
5) $-: Option parameters for the recently executed foreground pipeline
6) $$: The process ID itself
7) $!: PID to perform the previous background instruction
8) $_: Shows the last command executed

Bash shortcut keys

Emacs Style


Ctrl+p: Arrow key on ↑
CTRL + N: Under the arrow key ↓
Ctrl+b: Arrow key ←
ALT+F: The cursor moves right one word
Ctrl+f: Direction key →
ALT+B: Move the cursor left one word
CTRL + A: Cursor moves to the beginning of the line
Ctrl+e: Cursor moves to end of line
Ctrl+k: Clears the contents of the cursor to the end of the line.
Ctrl+d: Delete the letter of the cursor, note the difference between backspace and ctrl+h, and these 2 are the characters before the cursor is deleted.
Ctrl+r: Search for previously played commands. There will be a hint to search for the history of bash based on the keywords you entered
Ctrl+m: Enter carriage
Ctrl+i: Enter tab
ctrl+[: Enter ESC

Other
Ctrl+h: Deletes the previous character of the cursor, same as the BACKSPACE key.
ALT + P non-incremental reverse search history
The last line in the ALT + > History Command List starts forward
Ctrl+u: Clears all content before the cursor to the beginning of the line.
Ctrl+w: Remove a word before the cursor
Ctrl+t: Swap two characters before the cursor position
Ctrl+y: Paste or restore the last delete
Ctrl+l: Clear screen, equivalent to clear.
CTRL + XX cursor jumps at the head and end of line
Alt+r Undo all the contents of the current line
CTRL + Z: Turn the current process to run in the background
Ctrl+s: Lock the screen
Ctrl+q: Recovery screen
Ctrl + V key: Enter special characters
ALT + L converts the letters after the current cursor to lowercase letters
ALT + U converts the letter after the current cursor to uppercase
CTRL + ALT + E extends the contents of the command line (for example: ls = ls-l--color=tty)
CTRL + C: Kill the current process, input mode, interrupt the input command.
Ctrl+d: Exiting the current Shell
ESC +. Shortcut keys can poll the parameters or options of a history command.
The ESC + T shortcut can replace the first two words.
Enter a duplicate letter Esc {100} e to enter an e-character

Press multiple {ESC} to complete
{ESC} {~} can complement the user name on the machine
{ESC} {/} can complete the file name
{ESC} {@} can complete the hostname, and localhost can be conveniently used to complete the lo.


Bang Bang History Command
!! Re-execute the previous command
! n re-executes the nth command. Like!3.
!-n re-executes the reciprocal nth command.!-3
!string re-executes the command beginning with the string. Like!vim.
!?  String? Re-executes the command that contains the string. Like what!? Test.cpp?
!? string?% replaced by: The parameters of the command that recently contained this string. For example: Vim!? test.cpp?%
!$ replaced by: The last parameter of the previous command. Like Vim!$.
!! String appends a string to the previous command and executes.
! Nstring appends a string to the nth instruction and executes.
^old^new^ to replace the previous instruction
Modified

: s/old/new/replaces the first occurrence of new in the nth instruction with the old one. Like Vim!? test.cpp?:s/cpp/c/
: gs/old/new/Replace All
: WN W is a number and takes the first w parameter of the instruction.
:p echo commands instead of execution, such as!ls:p. This is very useful, you can first check the command you choose right, to execute when you use!!

Bash related files
/etc/profile Setting environment variables (all users)
~/.bash_profile setting environment variables (current user)
~/.bashrc
~/.bash_history
~/.bash_logout

Bash Usage Summary

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.