linux_sh/bash/shell_bash参考文档/查看可用shell /命令行编辑快捷键&技巧/shell job任务管理/job vs process

sh/bash/shell_bash参考文档

references

conclusion

  • sh 是一类命令行编程语言(满足POSIX标准)
  • bash 是sh的一种具体的实现程序
    • sh有其他的实现
    • 个人感觉有点想js和ecma之间的关系

What Is a Shell?

  • A shell is a computer program that takes commands, interprets them, and passes them to the operating system to process.
  • So, it’s an interface between the user and the operating system, through which a user can interact with the computer.
  • To interact with the shell, we need a terminal emulator such as gnome-terminal, konsole, or st.
  • Most Linux-based operating systems come with at least one shell program. The shell program will probably be Dash, Bash, or both.

user->terminal->shell->system

查看本机的可用shell程序

  • cat /etc/shells

sh

  • sh also known as Bourne Shell, is a command programming language for UNIX-like systems, defined by the POSIX standards .
  • sh can take input from either a keyboard or a file (commonly called a script file).
  • On most Linux systems, it’s implemented by programs like the original Bourne Shell , dash , and ksh.

sh on POSIX Systems

POSIX is a family of standards defined by IEEE for vendors to make operating systems compatible. Thus, it helps us develop cross-platform software for multiple operating systems by following a set of guidelines. *sh *conforms to these standards.

  • On most Linux systems, sh is a symlink to the actual implementation of Bourne Shell .
  • We can verify it through the following command:

某些发行版可能需要手动安装 file 命令

  • file: Determine type of FILEs.
    sudo apt install file
$ file -h /bin/sh

/bin/sh: symbolic link to dash
  • As we can see, the /bin/sh is symbolically linked to dash, which is a POSIX compliant shell used by Debian-based distributions. In shell scripts, we can put the #!/bin/sh, as the first line, which in turn will be executed by dash

Bash

  • Like sh , Bash (Bourne Again Shell) is a command language processor and a shell. It’s the default login shell on most Linux distributions.

  • Bash is a superset of sh, which means that Bash supports features of sh and provides more extensions on top of that.

    • Though, most of the commands work similarly as in sh .
  • Since the release of Bash, it’s been the de facto shell for Linux operating systems.

Which One to Use?

  • Both shells are useful, and we can leverage them in different situations. For instance, we can use sh if we want our script to be compatible across multiple systems. On the other hand, we might choose Bash because it provides a fluid syntax and more compelling features.
  • Nonetheless, we can use Bash and run it in pure POSIX mode if we are paranoid about portability and compatibility. Aside from that, if we write a sh script, it will most likely run on Bash without modification because Bash is backward-compatible with sh .

bash的常用功能

引号(单引号/双引号/backquote)

反引号(Command Substitution)

Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed as follows:

$(command)

or

`command`
  • Bash performs the expansion by executing command in a subshell environment and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat<span> </span><var>file</var>) can be replaced by the equivalent but faster $(<<span> </span><var>file</var>).

元字符

  • $
  • #
  • (space)
转义
  • 对元字符转移,可以阻止shell对字符的特殊解释(当成普通字符来看待)

通配

       Pattern Matching

       Any  character  that appears in a pattern, other than the special pattern charac‐
       ters described below, matches itself.  The NUL character may not occur in a  pat‐
       tern.   A  backslash  escapes  the following character; the escaping backslash is
       discarded when matching.  The special pattern characters must be quoted  if  they
       are to be matched literally.

       The special pattern characters have the following meanings:

              *      Matches  any  string, including the null string.  When the globstar
                     shell option is enabled, and * is used in a pathname expansion con‐
                     text, two adjacent *s used as a single pattern will match all files
                     and zero or more directories and subdirectories.  If followed by  a
                     /, two adjacent *s will match only directories and subdirectories.
              ?      Matches any single character.
              [...]  Matches  any  one of the enclosed characters.  A pair of characters
                     separated by a hyphen denotes a  range  expression;  any  character
                     that  falls between those two characters, inclusive, using the cur‐
                     rent locale's collating sequence and character set, is matched.  If
                     the first character following the [ is a !  or a ^ then any charac‐
                     ter not enclosed is matched.  The sorting order  of  characters  in
                     range  expressions is determined by the current locale and the val‐
                     ues of the LC_COLLATE or LC_ALL shell variables, if set.  To obtain
                     the traditional interpretation of range expressions, where [a-d] is
                     equivalent to [abcd], set value of the LC_ALL shell variable to  C,
                     or  enable the globasciiranges shell option.  A - may be matched by
                     including it as the first or last character in the set.  A ] may be
                     matched by including it as the first character in the set.

                     Within [ and ], character classes can be specified using the syntax
                     [:class:], where class is one of the following classes  defined  in
                     the POSIX standard:
                     alnum  alpha  ascii blank cntrl digit graph lower print punct space
                     upper word xdigit
                     A character class matches any character belonging  to  that  class.
                     The word character class matches letters, digits, and the character
                     _.

                     Within [ and ], an equivalence class can  be  specified  using  the
                     syntax  [=c=], which matches all characters with the same collation
                     weight (as defined by the current locale) as the character c.

                     Within [ and ], the syntax [.symbol.] matches the collating  symbol
                     symbol.

       If  the extglob shell option is enabled using the shopt builtin, several extended
       pattern matching operators are recognized.  In the following description, a  pat‐
       tern-list is a list of one or more patterns separated by a |.  Composite patterns
       may be formed using one or more of the following sub-patterns:

              ?(pattern-list)
                     Matches zero or one occurrence of the given patterns
              *(pattern-list)
                     Matches zero or more occurrences of the given patterns
              +(pattern-list)
                     Matches one or more occurrences of the given patterns
              @(pattern-list)
                     Matches one of the given patterns
              !(pattern-list)
                     Matches anything except one of the given patterns

       Complicated extended pattern matching against long strings  is  slow,  especially
       when  the patterns contain alternations and the strings contain multiple matches.
       Using separate matches against shorter strings, or using arrays  of  strings  in‐
       stead of a single long string, may be faster.

bash_命令行编辑快捷键&技巧/shell job任务管理/job vs process

reference

  • The Best Keyboard Shortcuts for Bash (aka the Linux and macOS Terminal) (howtogeek.com)

    • bash中的快捷键主要有Ctrl/Alt连个键打头
    • 同样的快捷键在不同上下文中,可能表现出不同的功能!
    • contents
    • Working With Processes
    • Controlling the Screen
    • Moving the Cursor
    • Deleting Text
    • Fixing Typos
    • Cutting and Pasting
    • Capitalizing Characters
    • Tab Completion
    • Working With Your Command History
    • emacs vs. vi Keyboard Shortcuts
  • bash(1) — Linux manual page

NAME |

SYNOPSIS |

COPYRIGHT | DESCRIPTION | OPTIONS | ARGUMENTS | INVOCATION | DEFINITIONS | RESERVED WORDS |

SHELL GRAMMAR | COMMENTS | QUOTING | PARAMETERS | EXPANSION | REDIRECTION | ALIASES | FUNCTIONS | ARITHMETIC EVALUATION | CONDITIONAL EXPRESSIONS | SIMPLE COMMAND EXPANSION | COMMAND EXECUTION | COMMAND EXECUTION ENVIRONMENT | ENVIRONMENT | EXIT STATUS | SIGNALS |

JOB CONTROL | PROMPTING | READLINE | HISTORY | HISTORY EXPANSION | SHELL BUILTIN COMMANDS | SHELL COMPATIBILITY MODE | RESTRICTED SHELL | SEE ALSO | FILES | AUTHORS | BUG REPORTS | BUGS | COLOPHON

编辑快捷键Moving the Cursor

Use the following shortcuts to quickly move the cursor around the current line while typing a command.

光标跳转

  • Ctrl+A or Home: Go to the beginning of the line.
  • Ctrl+E or End: Go to the end of the line.
  • Alt+B: Go left (back) one word.
    • Ctrl+B: Go left (back) one character.
  • Alt+F: Go right (forward) one word.
    • Ctrl+F: Go right (forward) one character.
  • Ctrl+XX: Move between the beginning of the line and the current position of the cursor.
    • This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position.
    • To use this shortcut, hold the Ctrl key and tap the X key twice.

文本修改Capitalizing Characters

The bash shell can quickly convert characters to upper or lower case:

  • Alt+U: Capitalize every character from the cursor to the end of the current word, converting the characters to upper case.
  • Alt+L: Uncapitalize every character from the cursor to the end of the current word, converting the characters to lower case.
  • Alt+C: Capitalize the character under the cursor. Your cursor will move to the end of the current word.
    • 可以用于将行内单词首字母大写化

进程管理快捷键

杀死当前进程(软杀)

  • Ctrl+C: Interrupt (kill) the current foreground process running in in the terminal.
    • This sends the SIGINT signal to the process, which is technically just a request—most processes will honor it, but some may ignore it.

挂起当前进程(暂停执行)

  • Ctrl+Z: Suspend the current foreground process running in bash. This sends the SIGTSTP signal to the process. To return the process to the foreground later, use the fg process_name command.

关闭/离开当前shell

  • 注意,只有在全新的一行输入Ctrl+D才是关闭shell

    Ctrl+D在不同上下文有不同的功能含义

    • 在zsh中,出现在若干字符后,键入Ctrl+D表示要求zsh,提供补全建议
    • 在光标处于某个字符下,ctrl+D表示删除该字符
  • Ctrl+D: Close the bash shell. This sends an EOF (End-of-file) marker to bash, and bash exits when it receives this marker. This is similar to running the exit command.

job vs process

      Job control refers to the ability to selectively stop (suspend)
       the execution of processes and continue (resume) their execution
       at a later point.  A user typically employs this facility via an
       interactive interface supplied jointly by the operating system
       kernel's terminal driver and bash.

       The shell associates a job with each pipeline.  It keeps a table
       of currently executing jobs, which may be listed with the jobs
       command.  When bash starts a job asynchronously (in the
       background), it prints a line that looks like:

              [1] 25647

       indicating that this job is job number 1 and that the process ID
       of the last process in the pipeline associated with this job is
       25647. 
       All of the processes in a single pipeline are members of the same job.  
       Bash uses the job abstraction as the basis for job control.
  • ┌─[cxxu@CxxuWin11] - [~] - [2022-04-20 08:44:08]
    └─[0] <> jobs
    [1]  - 9315 suspended (tty output)  man column |
           9316 suspended (tty output)  less
    [2]  + 9361 suspended (tty output)  man ls |
           9362 suspended (tty output)  less
    ┌─[cxxu@CxxuWin11] - [~] - [2022-04-20 08:44:26]
    └─[0] <> fg %2
    [2]  - 9361 continued  man ls |
           9362 continued  less
           
    # cxxu @ CxxuWin11 in ~ [8:51:47]
    $ man ls|grep 'r'|less
    
    [1]  + 9760 done       man ls |
           9761 done       grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox} 'r' |
           9763 suspended  less
    
    • 根据文档 All of the processes in a single pipeline are members of the same job.可知,一个job可以包括管道符链接起来的多个进程,这些进程同属于一个job号

Working With Your Command History

RELATED: *How to Use Your Bash History in the Linux or macOS Terminal*

You can quickly scroll through your recent commands, which are stored in your user account’s bash history file:

  • Ctrl+P or Up Arrow: Go to the previous command in the command history. Press the shortcut multiple times to walk back through the history.
  • Ctrl+N or Down Arrow: Go to the next command in the command history. Press the shortcut multiple times to walk forward through the history.
  • Alt+R: Revert any changes to a command you’ve pulled from your history if you’ve edited it.

Bash also has a special “recall” mode you can use to search for commands you’ve previously run:

  • Ctrl+R: Recall the last command matching the characters you provide. Press this shortcut and start typing to search your bash history for a command.
  • Ctrl+O: Run a command you found with Ctrl+R.
  • Ctrl+G: Leave history searching mode without running a command.

bash参考手册

Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]

posted @ 2022-03-22 22:08  xuchaoxin1375  阅读(32)  评论(0)    收藏  举报  来源