0
点赞
收藏
分享

微信扫一扫

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

芷兮离离 2022-06-14 阅读 89


文章目录

  • ​​sh/bash/shell_bash参考文档​​
  • ​​references​​
  • ​​conclusion​​
  • ​​What Is a Shell?​​
  • ​​查看本机的可用shell程序​​
  • ​​sh​​
  • ​​sh on POSIX Systems​​
  • ​​Bash​​
  • ​​Which One to Use?​​
  • ​​bash的常用功能​​
  • ​​引号(单引号/双引号/backquote)​​
  • ​​反引号(Command Substitution)​​
  • ​​元字符​​
  • ​​转义​​
  • ​​通配​​

  • ​​linux 安装powershell​​
  • ​​refereces​​
  • ​​Ubuntu/Debian install powershell7+​​
  • ​​检查当前发行版​​
  • ​​运行安装脚本​​
  • ​​verify installation​​

  • ​​bash_命令行编辑快捷键&技巧/shell job任务管理/job vs process​​
  • ​​reference​​
  • ​​编辑快捷键Moving the Cursor​​
  • ​​光标跳转​​
  • ​​文本修改Capitalizing Characters​​
  • ​​进程管理快捷键​​
  • ​​杀死当前进程(软杀)​​
  • ​​挂起当前进程(暂停执行)​​
  • ​​关闭/离开当前shell​​
  • ​​job vs process​​
  • ​​Working With Your Command History​​
  • ​​bash参考手册​​

sh/bash/shell_bash参考文档

references

  • ​​What’s the Difference Between sh and Bash? | Baeldung on Linux​​
  • ​​Bash Reference Manual (gnu.org)​​

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

  • shalso known as Bourne Shell, is acommand programming language for UNIX-like systems, defined by the POSIX standards.
  • shcan 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 originalBourne 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,shis 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

  • Likesh , 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 ofsh and provides more extensions on top of that.
  • Though, most of the commands work similarly as insh.
  • 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 useshif 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 withsh.

bash的常用功能

  • 使用​​man bash​​可以看到较为完整的bash介绍
  • ​​使用在线文档,方便浏览Bash Reference Manual (gnu.org)​​

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

  • ​​Quoting (Bash Reference Manual) (gnu.org)​​

反引号(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.

linux 安装powershell

refereces

  • ​​Install PowerShell on Linux - PowerShell | Microsoft Docs​​
  • ​​Alpine​​
  • ​​CentOS​​
  • ​​Debian​​
  • ​​Fedora​​

Ubuntu/Debian install powershell7+

  • ​​Installation via direct download​​
  • ​​Installation on Debian 11 via Package Repository​​
  • ​​Installation on Debian 10 via Package Repository​​
  • ​​Installation on Debian 9 via Package Repository​​

检查当前发行版

  • ​cat /etc/issue​
LSB Version:    core-9.20170808ubuntu1-noarch:security-9.20170808ubuntu1-noarch
Distributor ID: Ubuntu
Description: Ubuntu 18.04.6 LTS
Release: 18.04
Codename: bionic
Ubuntu 18.04.6 LTS \n \l

运行安装脚本

# Install system components
sudo apt update && sudo apt install -y curl gnupg apt-transport-https

# Import the public repository GPG keys
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

# Register the Microsoft Product feed
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-bullseye-prod bullseye main" > /etc/apt/sources.list.d/microsoft.list'

# Install PowerShell
sudo apt update && sudo apt install -y powershell

# Start PowerShell
pwsh

verify installation

  • ​cat /etc/shells|grep powershell​
# cxxu @ cxxuAli in ~ [11:04:44]
$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
/bin/zsh
/usr/bin/zsh
/usr/bin/pwsh
/opt/microsoft/powershell/7/pwsh

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+AorHome: Go to the beginning of the line.
  • Ctrl+EorEnd: 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 betweenthe beginning of the line andthe current position of the cursor.
  • This allows you to press Ctrl+XX toreturn to the start of the line, change something, andthen 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 characterfrom the cursor to the end of the current word, converting the characters to upper case.
  • Alt+L:Uncapitalizeevery characterfrom 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​​



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+PorUp Arrow: Go to the previous command in the command history. Press the shortcut multiple times to walk back through the history.
  • Ctrl+NorDown 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参考手册

  • ​​1 Introduction​​
  • ​​1.1 What is Bash?​​
  • ​​1.2 What is a shell?​​
  • ​​2 Definitions​​
  • ​​3 Basic Shell Features​​
  • ​​3.1 Shell Syntax​​
  • ​​3.1.1 Shell Operation​​
  • ​​3.1.2 Quoting​​
  • ​​3.1.2.1 Escape Character​​
  • ​​3.1.2.2 Single Quotes​​
  • ​​3.1.2.3 Double Quotes​​
  • ​​3.1.2.4 ANSI-C Quoting​​
  • ​​3.1.2.5 Locale-Specific Translation​​
  • ​​3.1.3 Comments​​
  • ​​3.2 Shell Commands​​
  • ​​3.2.1 Reserved Words​​
  • ​​3.2.2 Simple Commands​​
  • ​​3.2.3 Pipelines​​
  • ​​3.2.4 Lists of Commands​​
  • ​​3.2.5 Compound Commands​​
  • ​​3.2.5.1 Looping Constructs​​
  • ​​3.2.5.2 Conditional Constructs​​
  • ​​3.2.5.3 Grouping Commands​​
  • ​​3.2.6 Coprocesses​​
  • ​​3.2.7 GNU Parallel​​
  • ​​3.3 Shell Functions​​
  • ​​3.4 Shell Parameters​​
  • ​​3.4.1 Positional Parameters​​
  • ​​3.4.2 Special Parameters​​
  • ​​3.5 Shell Expansions​​
  • ​​3.5.1 Brace Expansion​​
  • ​​3.5.2 Tilde Expansion​​
  • ​​3.5.3 Shell Parameter Expansion​​
  • ​​3.5.4 Command Substitution​​
  • ​​3.5.5 Arithmetic Expansion​​
  • ​​3.5.6 Process Substitution​​
  • ​​3.5.7 Word Splitting​​
  • ​​3.5.8 Filename Expansion​​
  • ​​3.5.8.1 Pattern Matching​​
  • ​​3.5.9 Quote Removal​​
  • ​​3.6 Redirections​​
  • ​​3.6.1 Redirecting Input​​
  • ​​3.6.2 Redirecting Output​​
  • ​​3.6.3 Appending Redirected Output​​
  • ​​3.6.4 Redirecting Standard Output and Standard Error​​
  • ​​3.6.5 Appending Standard Output and Standard Error​​
  • ​​3.6.6 Here Documents​​
  • ​​3.6.7 Here Strings​​
  • ​​3.6.8 Duplicating File Descriptors​​
  • ​​3.6.9 Moving File Descriptors​​
  • ​​3.6.10 Opening File Descriptors for Reading and Writing​​
  • ​​3.7 Executing Commands​​
  • ​​3.7.1 Simple Command Expansion​​
  • ​​3.7.2 Command Search and Execution​​
  • ​​3.7.3 Command Execution Environment​​
  • ​​3.7.4 Environment​​
  • ​​3.7.5 Exit Status​​
  • ​​3.7.6 Signals​​
  • ​​3.8 Shell Scripts​​
  • ​​4 Shell Builtin Commands​​
  • ​​4.1 Bourne Shell Builtins​​
  • ​​4.2 Bash Builtin Commands​​
  • ​​4.3 Modifying Shell Behavior​​
  • ​​4.3.1 The Set Builtin​​
  • ​​4.3.2 The Shopt Builtin​​
  • ​​4.4 Special Builtins​​
  • ​​5 Shell Variables​​
  • ​​5.1 Bourne Shell Variables​​
  • ​​5.2 Bash Variables​​
  • ​​6 Bash Features​​
  • ​​6.1 Invoking Bash​​
  • ​​6.2 Bash Startup Files​​
  • ​​6.3 Interactive Shells​​
  • ​​6.3.1 What is an Interactive Shell?​​
  • ​​6.3.2 Is this Shell Interactive?​​
  • ​​6.3.3 Interactive Shell Behavior​​
  • ​​6.4 Bash Conditional Expressions​​
  • ​​6.5 Shell Arithmetic​​
  • ​​6.6 Aliases​​
  • ​​6.7 Arrays​​
  • ​​6.8 The Directory Stack​​
  • ​​6.8.1 Directory Stack Builtins​​
  • ​​6.9 Controlling the Prompt​​
  • ​​6.10 The Restricted Shell​​
  • ​​6.11 Bash POSIX Mode​​
  • ​​6.12 Shell Compatibility Mode​​
  • ​​7 Job Control​​
  • ​​7.1 Job Control Basics​​
  • ​​7.2 Job Control Builtins​​
  • ​​7.3 Job Control Variables​​
  • ​​8 Command Line Editing​​
  • ​​8.1 Introduction to Line Editing​​
  • ​​8.2 Readline Interaction​​
  • ​​8.2.1 Readline Bare Essentials​​
  • ​​8.2.2 Readline Movement Commands​​
  • ​​8.2.3 Readline Killing Commands​​
  • ​​8.2.4 Readline Arguments​​
  • ​​8.2.5 Searching for Commands in the History​​
  • ​​8.3 Readline Init File​​
  • ​​8.3.1 Readline Init File Syntax​​
  • ​​8.3.2 Conditional Init Constructs​​
  • ​​8.3.3 Sample Init File​​
  • ​​8.4 Bindable Readline Commands​​
  • ​​8.4.1 Commands For Moving​​
  • ​​8.4.2 Commands For Manipulating The History​​
  • ​​8.4.3 Commands For Changing Text​​
  • ​​8.4.4 Killing And Yanking​​
  • ​​8.4.5 Specifying Numeric Arguments​​
  • ​​8.4.6 Letting Readline Type For You​​
  • ​​8.4.7 Keyboard Macros​​
  • ​​8.4.8 Some Miscellaneous Commands​​
  • ​​8.5 Readline vi Mode​​
  • ​​8.6 Programmable Completion​​
  • ​​8.7 Programmable Completion Builtins​​
  • ​​8.8 A Programmable Completion Example​​
  • ​​9 Using History Interactively​​
  • ​​9.1 Bash History Facilities​​
  • ​​9.2 Bash History Builtins​​
  • ​​9.3 History Expansion​​
  • ​​9.3.1 Event Designators​​
  • ​​9.3.2 Word Designators​​
  • ​​9.3.3 Modifiers​​
  • ​​10 Installing Bash​​
  • ​​10.1 Basic Installation​​
  • ​​10.2 Compilers and Options​​
  • ​​10.3 Compiling For Multiple Architectures​​
  • ​​10.4 Installation Names​​
  • ​​10.5 Specifying the System Type​​
  • ​​10.6 Sharing Defaults​​
  • ​​10.7 Operation Controls​​
  • ​​10.8 Optional Features​​
  • ​​Appendix A Reporting Bugs​​
  • ​​Appendix B Major Differences From The Bourne Shell​​
  • ​​B.1 Implementation Differences From The SVR4.2 Shell​​
  • ​​Appendix C GNU Free Documentation License​​
  • ​​Appendix D Indexes​​
  • ​​D.1 Index of Shell Builtin Commands​​
  • ​​D.2 Index of Shell Reserved Words​​
  • ​​D.3 Parameter and Variable Index​​
  • ​​D.4 Function Index​​
  • ​​D.5 Concept Index​​

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


举报

相关推荐

0 条评论