Bash usage summary

  
Linux shell is basically bash by default, here are some tips I have summarized. & background running program () using a subshell, such as (cd ../../commlib/; make) $ () command replacement, and `` the role is the same as (command) the output of the command to a Temporary file << HereDoc use example: For example, you want to use the awk script awk -f <(cat <<EOF/abc/{print $0;} EOF )

$(()) in a shell script Perform integer calculation $((66/2))
if (( arithmetic operation)) if [[string operation]]alias Define the command alias dot. or source command, execute the script exec in the current shell to redirect the current The shell's file descriptor, or run another program. Trap can capture signal nohup to prevent problems caused by ssh hang screen can be used to keep the session, not affected by ssh shutdown export export variable to subshell using tee can offload output ENV_VAR=VALUE your_program This can modify its environment for this program Variable, external shell environment variable has not been changed tac inverted file directory jump cd - quickly return to the previous path cd back to the user's home directory pushd, popd, dirs implement multiple directory jump pushd command to change your current directory And store it on 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 displays the current directory stack. (dir –v –p)pushd +n; popd +n can operate the virtual directory stack and quickly jump to the common directory. You may already know that the $PATH variable can list the bash's "search path" & mdash;— When the current directory cannot find the requested file, bash will automatically search the directory. However, bash also supports the $CDPATH variable, which lists the directory to which the cd command is redirected when trying to change directories. To use this feature, we need to assign a directory listing 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 be redirected. Special parameters 1) $*: represents all parameters, the interval is the first character of the IFS default parameter. 2) $@: Same as the * asterisk. The difference is that IFS is not referenced. 3) $#: represents the number of parameters 4) $?: executes the return value of the previous instruction 5) $-: option parameter of the recently executed foreground pipeline 6) $$: Process ID 7 of its own $!: Execute the PID of the previous background command. 8) $_: Display the last executed command bash shortcut

Emacs style
ctrl+p: arrow keys ↑ ctrl+n: arrow keys ↓ ctrl+b: arrow keys ← alt+f: cursor right shift a word ctrl+f: arrow keys → alt+b: cursor left shift a word ctrl+a: cursor moves to the beginning of the line ctrl+e: cursor Move to the end of the line ctrl+k: Clear the contents of the cursor to the end of the line. Ctrl+d: Delete the letter where the cursor is located; note the difference between the backspace and ctrl+h. These two are the characters before the cursor. ctrl+r: The command that was hit before the search. There will be a prompt to search bash's history ctrl+m according to the keyword you entered: Enter carriage return ctrl+i : Enter tab ctrl+[ : Enter esc Other ctrl+h: Delete the previous character of the cursor, same as the backspace key. Alt + p Non-incremental reverse search history alt + > The last line of the history command list starts forward ctrl+u: Clears everything from the front of the cursor to the beginning of the line. Ctrl+w: Remove a word before the cursor ctrl+t: Swap the two characters before the cursor position ctrl+y: Paste or restore the last deletion ctrl+l: Clear screen, equivalent to clear. Ctrl + xx The cursor jumps at the beginning and end of the line alt+r Undo all the contents of the current line ctrl+z : Turn the current process to the background to run ctrl+s : Lock the screen ctrl+q : Restore screen ctrl+v key: Enter the special character alt + l to convert the letter after the current cursor to lowercase letter alt + u Convert the letter after the current cursor to the uppercase letter ctrl + Alt + e Expand the contents of the command line (eg ls => ls - l --color=tty) ctrl+c: Kill the current process, in the input mode, interrupt the input command. Ctrl+d: Exit the current shell esc + . The shortcut key can poll the parameters or options of the history command. The esc + t shortcut can replace the first two words. Enter the repeating letter Esc {100} e You can enter 100 e characters and press {esc} to complete {esc}{~} to complete the username on the machine {esc}{/} to complete the file name {esc }{@} can complete the host name, localhost can be easily completed with lo. Bang Bang history command!! Re-execute the previous command!N Re-execute the Nth command. For example, !3 !-N re-executes the last Nth command. !-3 !string Re-execute the command that starts with a string. For example, !vim !?string? Re-executes the command containing the string. For example, !?test.cpp? !?string?% is replaced with: The parameter of the command that recently contained this string. For example: vim !?test.cpp?% !$ is replaced with: the last parameter of the previous command. For example, vim !$ !!string appends string after the previous command and executes it. !Nstring appends string after the Nth instruction and executes it. ^old^new^ Replace the previous instruction. Modification: s/old/new/Replace the first occurrence of new in the Nth instruction with old. For example, vim !?test.cpp?:s/cpp/c/:gs/old/new/Replace all: wn w is a number, fetch the w parameter of the instruction. :p Echo command instead of execution, such as !ls :p. This is very useful, you can check the command you selected first, right, use it when you execute it!! Bash related files /etc/profile Set environment variables (all users) ~/.bash_profile Set environment variables (current user) ~/. Bashrc ~/.bash_history ~/.bash_logout

Copyright © Windows knowledge All Rights Reserved