{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/shell-unix-scripting-basics/",
  "atlas": "https://www.pystone.net/?node=shell-unix-scripting-basics#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/shell-unix-scripting-basics.md",
  "context": "https://www.pystone.net/notes/shell-unix-scripting-basics.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:shell-unix-scripting-basics",
  "slug": "shell-unix-scripting-basics",
  "title": "【batshell】shell on UNIX-like systems",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "【batshell】shell on UNIX like systems",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "db0208b17cff3e288f417618417587ea8e6e12472963f8390f70857a3adec4a7",
  "assets": [
    {
      "reference": "assets/0005 - 【bat_shell】shell on UNIX-like systems__截屏2020-10-23 上午11.19.51.png",
      "url": "/media/f935d15f0a14b8b05398.png",
      "mediaType": "image/png",
      "contentHash": "f935d15f0a14b8b053983b9f5b2376e202a5ae460ce12c07a6cb5ba681bc6f02",
      "byteLength": 41747,
      "width": 640,
      "height": 274
    }
  ],
  "headings": [
    {
      "depth": 1,
      "text": "【batshell】shell on UNIX-like systems",
      "anchor": "batshellshell-on-unix-like-systems",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#batshellshell-on-unix-like-systems"
    },
    {
      "depth": 2,
      "text": "Shell overview",
      "anchor": "shell-overview",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#shell-overview"
    },
    {
      "depth": 2,
      "text": "Linux中Shell的类型",
      "anchor": "linux中shell的类型",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#linux%E4%B8%ADshell%E7%9A%84%E7%B1%BB%E5%9E%8B"
    },
    {
      "depth": 2,
      "text": "查找命令",
      "anchor": "查找命令",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E6%9F%A5%E6%89%BE%E5%91%BD%E4%BB%A4"
    },
    {
      "depth": 2,
      "text": "grep命令",
      "anchor": "grep命令",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#grep%E5%91%BD%E4%BB%A4"
    },
    {
      "depth": 2,
      "text": "权限问题",
      "anchor": "权限问题",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E6%9D%83%E9%99%90%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 2,
      "text": "管理者权限",
      "anchor": "管理者权限",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E7%AE%A1%E7%90%86%E8%80%85%E6%9D%83%E9%99%90"
    },
    {
      "depth": 2,
      "text": "查看",
      "anchor": "查看",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E6%9F%A5%E7%9C%8B"
    },
    {
      "depth": 2,
      "text": "修改权限",
      "anchor": "修改权限",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E4%BF%AE%E6%94%B9%E6%9D%83%E9%99%90"
    },
    {
      "depth": 2,
      "text": "环境变量相关",
      "anchor": "环境变量相关",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E7%9B%B8%E5%85%B3"
    },
    {
      "depth": 2,
      "text": "环境变量分类",
      "anchor": "环境变量分类",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E5%88%86%E7%B1%BB"
    },
    {
      "depth": 3,
      "text": "对所有用户生效的永久性变量（系统级）",
      "anchor": "对所有用户生效的永久性变量系统级",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E5%AF%B9%E6%89%80%E6%9C%89%E7%94%A8%E6%88%B7%E7%94%9F%E6%95%88%E7%9A%84%E6%B0%B8%E4%B9%85%E6%80%A7%E5%8F%98%E9%87%8F%E7%B3%BB%E7%BB%9F%E7%BA%A7"
    },
    {
      "depth": 3,
      "text": "对单一用户生效的永久性变量（用户级）",
      "anchor": "对单一用户生效的永久性变量用户级",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E5%AF%B9%E5%8D%95%E4%B8%80%E7%94%A8%E6%88%B7%E7%94%9F%E6%95%88%E7%9A%84%E6%B0%B8%E4%B9%85%E6%80%A7%E5%8F%98%E9%87%8F%E7%94%A8%E6%88%B7%E7%BA%A7"
    },
    {
      "depth": 3,
      "text": "临时有效的环境变量（只对当前shell有效）",
      "anchor": "临时有效的环境变量只对当前shell有效",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E4%B8%B4%E6%97%B6%E6%9C%89%E6%95%88%E7%9A%84%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E5%8F%AA%E5%AF%B9%E5%BD%93%E5%89%8Dshell%E6%9C%89%E6%95%88"
    },
    {
      "depth": 2,
      "text": "常用环境变量",
      "anchor": "常用环境变量",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E5%B8%B8%E7%94%A8%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"
    },
    {
      "depth": 2,
      "text": "设置环境变量常用的几个指令",
      "anchor": "设置环境变量常用的几个指令",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E8%AE%BE%E7%BD%AE%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E5%B8%B8%E7%94%A8%E7%9A%84%E5%87%A0%E4%B8%AA%E6%8C%87%E4%BB%A4"
    },
    {
      "depth": 3,
      "text": "变量的引用",
      "anchor": "变量的引用",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E5%8F%98%E9%87%8F%E7%9A%84%E5%BC%95%E7%94%A8"
    },
    {
      "depth": 3,
      "text": "命令替换：$(command)",
      "anchor": "命令替换command",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E5%91%BD%E4%BB%A4%E6%9B%BF%E6%8D%A2command"
    },
    {
      "depth": 2,
      "text": "进程相关技巧",
      "anchor": "进程相关技巧",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E8%BF%9B%E7%A8%8B%E7%9B%B8%E5%85%B3%E6%8A%80%E5%B7%A7"
    },
    {
      "depth": 2,
      "text": "压缩解压",
      "anchor": "压缩解压",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E5%8E%8B%E7%BC%A9%E8%A7%A3%E5%8E%8B"
    },
    {
      "depth": 2,
      "text": "概览",
      "anchor": "概览",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E6%A6%82%E8%A7%88"
    },
    {
      "depth": 2,
      "text": "zip / unzip 命令",
      "anchor": "zip-unzip-命令",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#zip-unzip-%E5%91%BD%E4%BB%A4"
    },
    {
      "depth": 2,
      "text": "拓展延伸 Console, TTY等",
      "anchor": "拓展延伸-console-tty等",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#%E6%8B%93%E5%B1%95%E5%BB%B6%E4%BC%B8-console-tty%E7%AD%89"
    },
    {
      "depth": 2,
      "text": "ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/shell-unix-scripting-basics/#ref"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:software-engineering-and-quality",
      "title": "软件工程与质量保障",
      "url": "https://www.pystone.net/notes/software-engineering-and-quality/",
      "atlas": "https://www.pystone.net/?node=software-engineering-and-quality#knowledge-atlas",
      "label": "软件工程与质量保障",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "CI-CD与质量保障/CI-CD工具链中的“【batshell】shell on UNIX-like systems”导航项",
      "citation": "https://www.pystone.net/notes/software-engineering-and-quality/#ci-cd%E4%B8%8E%E8%B4%A8%E9%87%8F%E4%BF%9D%E9%9A%9Cci-cd%E5%B7%A5%E5%85%B7%E9%93%BE"
    }
  ],
  "contentMarkdown": "# 【batshell】shell on UNIX-like systems\n\n> 创建时间：2020/10/21 14:46\n\n  * 【bat/shell】shell on UNIX-like systems\n  * Shell overview\n    * Linux中Shell的类型\n  * 查找命令\n  * grep命令\n  * 权限问题\n    * 管理者权限\n    * 查看\n    * 修改权限\n  * 环境变量相关\n    * 环境变量分类\n      * 对所有用户生效的永久性变量（系统级）\n      * 对单一用户生效的永久性变量（用户级）\n      * 临时有效的环境变量（只对当前shell有效）\n    * 常用环境变量\n    * 设置环境变量常用的几个指令\n      * 变量的引用\n      * 命令替换：$(command)\n  * 进程相关技巧\n  * 压缩解压\n    * 概览\n    * zip / unzip 命令\n  * 拓展延伸 Console, TTY等\n    * ref\n\n## Shell overview\n\nshell即“壳”，是用户和操作系统（系统内核）之间的接口程序，是个命令解释程序，拥有自己内建的 shell 命令集。提示符下输入的每个命令都由shell先解释然后传给操作系统内核。\n\nLinux下有Bash、Zsh等，Windows下有CMD、PowerShell等，都是Shell。\n\n## Linux中Shell的类型\n\nLinux 中的 shell 有很多类型，其中最常用的几种是: Bourne shell (sh)、C shell (csh) 和 Korn shell (ksh), 各有优缺点。Bourne shell 是 UNIX 最初使用的 shell，并且在每种 UNIX 上都可以使用, 在 shell 编程方面相当优秀，但在处理与用户的交互方面做得不如其他几种shell。Linux 操作系统缺省的 shell 是Bourne Again shell，它是 Bourne shell 的扩展，简称 Bash，与 Bourne shell 完全向后兼容，并且在Bourne shell 的基础上增加、增强了很多特性。Bash放在/bin/bash中，它有许多特色，可以提供如命令补全、命令编辑和命令历史表等功能，它还包含了很多 C shell 和 Korn shell 中的优点，有灵活和强大的编程接口，同时又有很友好的用户界面。\n\n<https://blog.csdn.net/MonMama/article/details/53390610>\n<https://blog.csdn.net/wenlifu71022/article/details/4069929>\n\n## 查找命令\n\n<http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html>\n\n## grep命令\n\n## 权限问题\n\n## 管理者权限\n\n**sudo命令** \\- 以系统管理者的身份执行指令，也就是说，经由 sudo 所执行的指令就好像是 root 亲自执行。\n使用权限：在 /etc/sudoers 中有出现的使用者。\n\n**su** 命令为切换用户的命令。\n\n切换为root用户执行命令：\n`sudo su root`或·`sudo su`: 切换到root用户，不改变当前变量\n`sudo su - root`或·`sudo su -`: 切换到root用户，并获得root的环境变量及执行权限\n只要当前用户为管理员用户，即可使用sudo切换到root用户，此时输入的密码为当前管理员用户的密码。\n\n若直接使用命令`su root`，则需要输入的密码为root用户的密码。输入当前管理员用户的密码会返回 su: Sorry。\n可以使用管理员权限修改root用户的密码后，再执行`su root`命令，这样输入刚刚修改好的root密码即可切换到root用户如下图：\n\n![Alt text](assets/0005 - 【bat_shell】shell on UNIX-like systems__截屏2020-10-23 上午11.19.51.png)\n\n## 查看\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n权限说明一共10位: drwxrwxrwx\n第一位 - 文件类型，” + “代表目录，” - “代表非目录。\n2-4位 - 所有者user的权限说明\n5-7位 - 组群group的权限说明\n8-10位 - 其他人other的权限说明\n\nr代表可读权限，w代表可写权限，x代表可执行权限。\n\n> MacOS上，权限说明后可能有 + 或 @\n>  ‘+’ 表示扩展属性(extended attributes)\n>  ‘@’ 表示扩展安全信息(extended security information)\n>  详情可通过命令 ls -le 或 ls -l@ 查看\n\n## 修改权限\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n授予其他人写xxx.xxx这个文件的权限\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n删除xxx.xxx中组群和其他人的读和写的权限\n\n> u 代表所有者（user）\n>  g 代表所有者所在的组群（group）\n>  o 代表其他人，但不是u和g （other）\n>  a 代表全部的人，也就是包括u，g和o\n\nr w x可以换成数字表示：\nr 对应 4， w 对应 2， x 对应 1\nrwx 或 - 的任意组合对应的数字是唯一的，所以可用一个数字表示一组rwx权限。\n如 7 = 4+2+1 表示赋予目录可读可写可执行权限\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n## 环境变量相关\n\n环境变量是在操作系统中一个具有特定名字的对象，它包含了一个或多个应用程序将使用到的信息。Linux是一个多用户的操作系统，每个用户登录系统时都会有一个专用的运行环境。这个默认环境就是一组环境变量的定义。每个用户都可以通过修改环境变量的方式对自己的运行环境进行配置。\n\n## 环境变量分类\n\n### 对所有用户生效的永久性变量（系统级）\n\n位置：/etc/profile\n添加方式：vim打开/etc/ profile文件，用export指令添加环境变量。\n调用`source /etc/profile`才能立即生效，否则下次进入此用户生效。\n\n### 对单一用户生效的永久性变量（用户级）\n\n设置方法：在用户主目录”~”下的隐藏文件 “.bashrc”中添加自己想要的环境变量。\n同样调用`source ./.bashrc`该文件才会生效。否则只能在下次重进此用户时才能生效。\n\n>   * ~/.bash_profile是交互式login方式进入bash shell运行，只会在用户登录的时候读取一次。\n>\n>   * ~/ .bashrc是交互式non-login方式进入bash shell运行，在每次打开终端进行一次新的会话时都会读取。\n>\n>\n\n### 临时有效的环境变量（只对当前shell有效）\n\n此类环境变量只对当前的shell有效。当我们退出登录或者关闭终端再重新打开时，这个环境变量就会消失。是临时的。\n设置方法：直接使用export指令添加。 如`export PATH=/usr/local/bin:$PATH`\n\n## 常用环境变量\n\n  * PPID: 是当前进程的父进程的PID\n\n  * PWD: 当前工作目录。命令pwd的输出该变量值。\n\n  * RANDOM: 随机数变量。每次引用这个变量会得到一个0~32767的随机数。\n\n  * SECONDS: 脚本已经运行的时间（以秒为单位）\n\n  * PATH 指定命令的搜索路径。通过设置环境变量PATH可以让我们运行程序或指令更加方便。\n\n> 每一个冒号都是一个路径，这些搜索路径都是一些可以找到可执行程序的目录列表。当我们输入一个指令时，shell会先检查命令是否是内部命令，不是的话会再检查这个命令是否是一个应用程序。然后shell会试着从搜索路径，即PATH中寻找这些应用程序。如果shell在这些路径目录里没有找到可执行文件。则会报错。若找到，shell内部命令或应用程序将被分解为系统调用并传给Linux内核。\n\n  * SHELL 指当前用户用的是哪种shell\n\n## 设置环境变量常用的几个指令\n\n  * echo - 查看显示环境变量，变量使用时要加上符号“$”\n`echo $PATH`\n\n  * export - 设置新的环境变量\n`export PATH=/usr/local/bin:$PATH`\n\n  * 修改环境变量 - 修改环境变量没有指令，可以直接使用环境变量名进行修改。\n`MYNAME=\"Prin\"`\n\n  * env 查看所有环境变量\n\n  * set 查看本地定义的所有shell变量\n\n  * unset 删除一个环境变量\n\n  * readonly 设置只读环境变量\n`readonly MYNAME`\n\n### 变量的引用\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n### 命令替换：$(command)\n\n命令替换的目的是获取命令的输出、为变量赋值，或对命令的输出做进一步的处理。\n\n## 进程相关技巧\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n<https://www.runoob.com/linux/linux-comm-grep.html>\ngrep 命令用于查找文件里符合条件的字符串。上条命令可用于查找包含特定字符串的进程\n\n如果出现进程未响应的状况，则查找到进程的PID后，可调用`kill -9 PID`强制终止进程\n\n## 压缩解压\n\n## 概览\n\n.tar\n解包：tar xvf FileName.tar\n打包：tar cvf FileName.tar DirName\n（注：tar是打包，不是压缩！）\n\n.gz\n解压1：gunzip FileName.gz\n解压2：gzip -d FileName.gz\n压缩：gzip FileName\n.tar.gz 和 .tgz\n解压：tar zxvf FileName.tar.gz\n压缩：tar zcvf FileName.tar.gz DirName\n\n.bz2\n解压1：bzip2 -d FileName.bz2\n解压2：bunzip2 FileName.bz2\n压缩： bzip2 -z FileName\n.tar.bz2\n解压：tar jxvf FileName.tar.bz2\n压缩：tar jcvf FileName.tar.bz2 DirName\n\n.bz\n解压1：bzip2 -d FileName.bz\n解压2：bunzip2 FileName.bz\n压缩： 未知\n.tar.bz\n解压：tar jxvf FileName.tar.bz\n压缩： 未知\n\n.Z\n解压：uncompress FileName.Z\n压缩：compress FileName\n.tar.Z\n解压：tar Zxvf FileName.tar.Z\n压缩：tar Zcvf FileName.tar.Z DirName\n\n.zip\n解压：unzip FileName.zip\n压缩：zip FileName.zip DirName\n\n.rar\n解压：rar x FileName.rar\n压缩：rar a FileName.rar DirName\n\n## zip / unzip 命令\n\nunzip指令 格式如下：\n\n//待更\n\n-d 目录名 将压缩文件解压到指定目录下\n\n## 拓展延伸 Console, TTY等\n\n<https://www.zhihu.com/question/26860370>\n\n## ref\n\n<https://www.iteye.com/blog/henry-cong-1060014>\n<https://www.jianshu.com/p/09f521c60c30>\n<https://blog.csdn.net/Axela30W/article/details/78981749>\n<https://blog.csdn.net/huayangshiboqi/article/details/80150842>\n"
}
