既完成 VIM 的学习之后,上周又找到一个非常好的关于 BASH 的学习材料,通过刻意练习,并反复实践加深记忆,使得命令行上手更加轻松。
以下是学习内容中的第一部分:
The Art of Command Line
Fluency on the command line is a skill often neglected or considered arcane, but it improves your flexibitlity and productivity as an engineer in both obvious and subtle ways. This is a selection of notes and tips on using the command-line that we've found useful when working on Linux. Some tips are elementary, and some are fairly specific, sophisticated, or obscure. This page is not long, but if you can use and recall all the items here, you know a lot.
This work is the result of many authors and translators. Some of this originally appeared on Quora, but it has since moved to GitHub, where people more talented than the original author have made numerous improvements. Please submit a question if you have a question related to the command line. Please contribute if you see an error or something that could be better!
Meta
Scope:
- This guide is both for beginners and the experienced. The goals are breadth (everything important), specificity (give concrete examples of the most common case), and brevity (avoid things that aren't essential or digressions you can easily look up elsewhere). Every tip is essential in some situation or significantly saves time over alternatives.
- This is written for Linux, with the exception of the "macOS only" and "Windows only" sections. Many of the other items apply or can be installed on other Unices or macOS (or even Cygwin).
- The focus is on interactive Bash, though many tips apply to other shells and to general Bash scripting.
- It includes both "standard" Unix commands as well as ones that require special package installs -- so long as they are important enough to merit inclusion.
Notes:
- To keep this to one page, content is implicitly included by reference. Yor're smart enough to look up more detail elsewhere once you know the idea or command to Google. Use
apt
,yum
,dnf
,pacman
,pip
orbrew
(as appropriate) to install new programs. - Use Explainshell to get a helpful breakdown of what commands, options, pipes etc. do.
Basics
Learn basic Bash. Actually, type
man bash
and at least skim the whole thing; it's pretty easy to follow and not that long. Alternate shells can be nice, but Bash is powerful and always available (learning only zsh, fish, etc., while tempting on your own laptop, restricts you in many situations, such as using existing servers).Learn at least one text-based editor well. The
nano
editor is one of the simplest for basic editing (opening, editing, saving, searching). However, for the power user in a text terminal, there is no substitute for Vim (vi
), the hard-to-learn but venerable, fast, and full-featured editor. Many people also use the classic Emacs, particularly for larger editing tasks. (Of course, any modern software developer working on an extensive project is unlikely to use only a pure text-based editor and should also be familiar with modern graphical IDEs and tools.)-
Finding documentation:
- Know how to read official documentation with
man
(for the inquisitive,man man
lists the section numbers, e.g. 1 is "regular" commands, 5 is files/conventions, and 8 are for administration). Find man pages withapropos
. - Know that some commands are not executables, but Bash builtins, and that you can get help on them with
help
andhelp -d
. You can find out whether a command is an executable, shell builtin or an alias by usingtype command
. -
curl cheat.sh/command
will give a brief "cheat sheet" with common examples of how to use a shell command.
- Know how to read official documentation with
Learn about redirection of output and input using
>
and<
and pipes using|
. Know>
overwrites the output file and>>
appends. Learn about stdout and stderr.Learn about file glob expansion with
*
(and perhaps?
and[
...]
) and quoting and the difference between double"
and single'
quotes. (See more on variable expansion below.)Be familiar with Bash job management:
&
, ctrl-z, ctrl-c,jobs
,fg
,bg
,kill
, etc.Know
ssh
, and the basics of passwordless authentication, viassh-agent
,ssh-add
, etc.Basic file management:
ls
andls -l
(in particular, learn what every column inls -l
means),less
,head
,tail
andtail -f
(or even better,less +F
),ln
andln -s
(learn the differences and advantages of hard versus soft links),chown
,chmod
,du
(for a quick summary of disk usage:du -hs *
). For filesystem management,df
,mount
,fdish
,mkfs
,lsblk
. Learn what an inode is (ls -i
ordf -i
).Basic network management:
ip
orifconfig
,dig
,traceroute
,route
.Learn and use a version control management system, such as
git
.Know regular expressions well, and the various flags to
grep
/egrep
. The-i
,-o
,-v
,-A
,-B
, and-C
options are worth knowing.Learn to use
apt-get
,yum
,dnf
orpacman
(depending on distro) to find and install packages. And make sure you havepip
to install Python-based command-line tooles (a few below are easiest to install viapip
).
Everyday use
In Bash, use Tab to complete arguments or list all available commands and ctrl-r to search through command history (after pressing, type to search, press ctrl-r repeatedly to cycle through more matches, press Enter to execute the found command, or hit the right arrow to put the result in the current line to allow editing).
In Bash, use ctrl-w to delete the last word, and ctrl-u to delete the content from current cursor back to the start of the line. Use alt-b and alt-f to move by word, ctrl-a to move cursor to begining of line, ctrl-e to move cursor to end of line, ctrl-k to kill to the end of the line, ctrl-l to clear the screen. See
man readline
for all the default keybindings in Bash. There are a lot. For example alt-. cycles through previous arguments, and alt-* expands a glob.Alternatively, if you love vi-style key-bindings, use
set -o vi
(andset -o emacs
to put it back).For editing long commands, after setting your editor (for example
export EDITOR=vim
), ctrl-x ctrl-e will open the current command in an editor for multi-line editing. Or in vi style, escape-v.To see recent commands, use
history
. Follow with!n
(wheren
is the command number) to execute again. There are also many abbreviations you can use, the most useful probably being!$
for last argument and!!
for last command (see "HISTORY EXPANSION" in the man page). However, these are often easily replaced with ctrl-r and alt-..Go to your home directory with
cd
. Access files relative to your home directory with the~
prefix (e.g.~/.bashrc
). Insh
scripts refer to the home directory as$HOME
.To go back to the previous working directory:
cd -
.If you are halfway through typing a command but change your mind, hit alt-# to add a
#
at the beginning and enter it as a comment (or use ctrl-a, #, enter). You can then return to it later via command history.Use
xargs
(orparallel
). It's very powerful. Note you can control how many items execute per line (-L
) as well as parallelism (-P
). If you're not sure if it'll do the right thing, usexargs echo
first. Also,-I{}
is handy.
Examples:
find . -name '*.py' | xargs grep some_function
cat hosts | xargs -I{} ssh root@{} hostname
pstree -p
is a helpful display of the process tree.Use
pgrep
andpkill
to find or signal processes by name (-f
is helpful).Know the various signals you can send processes. For example, to suspend a process, use
kill -STOP [pid]
. For the full list, seeman 7 signal
Use
nohup
ordisown
if you want a background process to keep running forever.Check what processes are listening via
netstat -lntp
orss -plat
(for TCP; add-u
for UDP) orlsof -iTCP -sTCP:LISTEN -P -n
(which also works on macOS).See also
lsof
andfuser
for open sockets and files.See
uptime
orw
to know how long the system has been running.Use
alias
to create shortcuts for commonly used commands. For example,alias ll='ls -latr'
creates a new aliasll
.Save aliases, shell settings, and functions you commonly use in
~/.bashrc
, and arrange for login shells to source it. This wil make yuour setup available in al your shell sessions.Put the settings of environment variables as well as commands that should be executed when you login in
~/.bash_profile
. Separate configuration will be needed for shells you launch from graphical environment logins andcron
jobs.Synchronize your configuration files (e.g.
.bashrc
and.bash_profile
) among various computers with Git.Understand that care is needed when variables and filenames include whitespace. Surround your Bash variables with quotes, e.g.
"$F00"
. Prefer the-0
or-print0
options to enable null characters to delimit filenames, e.g.locate -0 pattern | xargs -0 ls -al
orfind / -print0 -type d | xargs -0 ls -al
. To iterate on filenames containing whitespace in a for loop, set your IFS to be a newline only usingIFS=$'\n'
.In Bash scripts, use
set -x
(or the variantset -v
, which logs raw input, including unexpanded variables and comments) for debugging output. Use strict modes unless you have a good reason not to: Useset -e
to abort on errors (nonzero exit code). Useset -u
to detect unset variable usages. Considerset -o pipefail
too, to abort on errors within pipes (though read up on it more if you do, as this topic is a bit subtle). For more involved scripts, also usetrap
on EXIT or ERR. A useful habit is to start a script like this, which will make it detect and abort on common errors and print a message:
set -euo pipefail
trap "echo 'error: Script failed: see failed command above'" ERR