领悟命令行的艺术1

从man bash开始

-c If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, the first argument is assigned to $0 and any remaining argu‐ ments are assigned to the positional parameters. The assignment to $0 sets the name of the shell, which is used in warning and error messages.

-c参数,指明非选项参数后的第一个命令参数为$0,后面的参数依次赋值给对应的位置参数。而且$0会作为shell的名称,用来展示警告和错误信息。

-s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.

-s 参数用于在使用交互式shell时,指明位置参数

–norc Do not read and execute the system wide initialization file /etc/bash.bashrc and the personal initialization file ~/.bashrc if the shell is interactive. This option is on by default if the shell is invoked as sh.

这个–norc 的参数说明中,讲解了,在使用sh时,会默认设置–norc参数,这将导致shell不会加载系统的/etc/bash.bashrc文件和用户的.bashrc文件。这里我理解为在脚本中写的#!/bin/sh,会使得该脚本运行时无法使用定义在bashrc中的变量。

A login shell is one whose first character of argument zero is a -, or one started with the --login option.

这句说明,涉及到知识点su user 和su - user的差别。登录式shell

An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

这里的说明讲解的时交互式shell的定义。启动一个没有参数(不包括 -s参数)且不包含-c参数的标准输入输出的termial。

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it
looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

交互式shell或者使用了login参数的非交互式shell,第一次登录时会读取和执行/etc/profile文件和用户目录下的.bash_profile、.bash_login、.profile文件。而–noprofile参数会禁止这个行为。

When an interactive login shell exits, or a non-interactive login shell executes the exit builtin command, bash reads and executes commands from the file ~/.bash_logout, if it exists.

交互式shell或者一个登录的非交互式shell,退出时会执行.bash_logout文件

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

交互式shell启动时还会加载.bashrc文件。–norc取消这一行为,–rcfile强制指定要执行的文件来替代bash.bashr和.bashrc文件

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.

如果使用名称sh调用bash,它会尝试尽可能接近地模仿sh的历史版本的启动行为,同时也符合POSIX标准。 当作为交互式登录shell或带有–login选项的非交互式shell调用时,它首先尝试按顺序从/ etc / profile和〜/ .profile中读取和执行命令。 --noprofile选项可用于禁止此行为。 当作为具有名称sh的交互式shell调用时,bash查找变量ENV,如果已定义则展开其值,并使用扩展值作为要读取和执行的文件的名称。 由于以sh调用的shell不会尝试从任何其他启动文件读取和执行命令,因此–rcfile选项无效。 使用名称sh调用的非交互式shell不会尝试读取任何其他启动文件。 当调用为sh时,bash在读取启动文件后进入posix模式。
这里的sh 值得就是我们执行脚本时使用的sh scripts.sh

交互式和非交互式

bash手册中多次提到交互式和非交互式。
当我们使用shell,有user@host:~$,表明我们使用的是交互式shell。
什么时候使用的是非交互式?
一个shell脚本执行的时候,实际上是开启了一个非交互式的shell去执行相关的命令。
从手册我们关注到一个要点就是,非交互式shell使用的env变量。

主要的元字符(不被引号引起)

| & ; ( ) < > space tab newline

操作符

|| & && ; ;; ;& ;;& ( ) | |& <newline>

保留字

! case coproc do done elif else esac fi for function if in select then until while { } time [[ ]]

中文版辅助

https://blog.pytool.com/linux/shell/handbook/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值