Terminal Tricks
2023년 8월 11일About 3 min
Terminal Tricks 관련
Shell > Article(s)
Article(s)
Terminal Tricks | Bash-Oneliner
A collection of handy Bash One-Liners and terminal tricks for data processing and Linux system maintenance.
Using Ctrl keys
- Ctrl+a: move to the beginning of line.
- Ctrl+d: if you've type something, Ctrl + d deletes the character under the cursor, else, it escapes the current shell.
- Ctrl+e: move to the end of line.
- Ctrl+k: delete all text from the cursor to the end of line.
- Ctrl+l: equivalent to clear.
- Ctrl+n: same as Down arrow.
- Ctrl+p: same as Up arrow.
- Ctrl+q: to resume output to terminal after Ctrl + s.
- Ctrl+r: begins a backward search through command history.(keep pressing Ctrl + r to move backward)
- Ctrl+s: to stop output to terminal.
- Ctrl+t: transpose the character before the cursor with the one under the cursor, press Esc + t to transposes the two words before the cursor.
- Ctrl+u: cut the line before the cursor; then Ctrl + y paste it
- Ctrl+w: cut the word before the cursor; then Ctrl + y paste it
- Ctrl+x+backspace : delete all text from the beginning of line to the cursor.
- Ctrl+x+Ctrl+e : launch editor defined by $EDITOR to input your command. Useful for multi-line commands.
- Ctrl+z: stop current running process and keep it in background. You can use
fg
to continue the process in the foreground, orbg
to continue the process in the background. - Ctrl+_: undo typing.
Change case
- Esc+u: converts text from cursor to the end of the word to uppercase.
- Esc+l: converts text from cursor to the end of the word to lowercase.
- Esc+c: converts letter under the cursor to uppercase, rest of the word to lowercase.
Run history number
e.g.
53
!53
Run last command
!!
# run the previous command using sudo
sudo !!
Run last command and change some parameter using caret substitution
e.g. last command:
echo 'aaa'
-> rerun as:echo 'bbb'
#last command: echo 'aaa'
^aaa^bbb
#echo 'bbb'
#bbb
#Notice that only the first aaa will be replaced, if you want to replace all 'aaa', use ':&' to repeat it:
^aaa^bbb^:&
#or
!!:gs/aaa/bbb/
Run past command that began with
e.g.
cat filename
!cat
# or
!c
# run cat filename again
Bash globbing
# '*' serves as a "wild card" for filename expansion.
/etc/pa*wd #/etc/passwd
# '?' serves as a single-character "wild card" for filename expansion.
/b?n/?at #/bin/cat
# '[]' serves to match the character from a range.
ls -l [a-z]* #list all files with alphabet in its filename.
# '{}' can be used to match filenames with more than one patterns
ls *.{sh,py} #list all .sh and .py files
Some handy environment variables
$0 :name of shell or shell script.
$1, $2, $3, ... :positional parameters.
$# :number of positional parameters.
$? :most recent foreground pipeline exit status.
$- :current options set for the shell.
$$ :pid of the current shell (not subshell).
$! :is the PID of the most recent background command.
$_ :last argument of the previously executed command, or the path of the bash script.
$DESKTOP_SESSION current display manager
$EDITOR preferred text editor.
$LANG current language.
$PATH list of directories to search for executable files (i.e. ready-to-run programs)
$PWD current directory
$SHELL current shell
$USER current username
$HOSTNAME current hostname
Using vi-mode in your shell
set -o vi
# change bash shell to vi mode
# then hit the Esc key to change to vi edit mode (when `set -o vi` is set)
k # in vi edit mode - previous command
j # in vi edit mode - next command
0 # in vi edit mode - beginning of the command
R # in vi edit mode - replace current characters of command
2w # in vi edit mode - next to 2nd word
b # in vi edit mode - previous word
i # in vi edit mode - go to insert mode
v # in vi edit mode - edit current command in vi
man 3 readline # man page for complete readline mapping