bash 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

bash 命令安装:



why-i-love-bash

-bash/zsh: bash: command not found

# Windows (WSL2)
sudo apt-get update sudo apt-get install bash

# Debian
apt-get install bash

# Ubuntu
apt-get install bash

# Alpine
apk add bash

# Arch Linux
pacman -S bash

# Kali Linux
apt-get install bash

# CentOS
yum install bash

# Fedora
dnf install bash

# OS X
brew install bash

# Raspbian
apt-get install bash

# Dockerfile
 dockerfile.run/bash

# Docker
docker run cmd.cat/bash bash

bash 命令补充说明:


bash 命令 - Bourne-Again SHell,是一个与 sh 兼容的命令行解释器。 另请参见:zsh、histexpand(历史扩展)。


bash-tricks


more-bash-tricks

bash 是一种与 sh 兼容的命令语言解释器,它执行从标准输入或文件读取的命令。 Bash 还融合了 KornC shell 的有用功能(kshcsh)。

bash 旨在成为 IEEE POSIX 规范(IEEE 标准 1003.1)的 Shell 和实用程序部分的一致实现。 Bash 可以通过以下方式配置为符合 POSIX - 默认


environment-variables

bash 命令语法:


bash [options] [file]
bash [GNU long option] [option] ...
bash [GNU long option] [option] script-file ...

bash 命令选项:


All of the  single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked.  In addition, bash interprets the follow‐ing options when it is invoked:

-c string If the -c option is present, then commands are read from string.  If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
-i        If the -i option is present, the shell is interactive.
-l        Make bash act as if it had been invoked as a login shell (see INVOCATION below).
-r        If the -r option is present, the shell becomes restricted (see RESTRICTED SHELL below).
-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.
-D        A list of all double-quoted strings preceded by $ is printed on the standard output.  These are the strings that are subject to language translation when the current locale is not C
         or POSIX.  This implies the -n option; no commands will be executed.
[-+]O [shopt_option]
         shopt_option is one of the shell options accepted by the shopt builtin (see SHELL BUILTIN COMMANDS below).  If shopt_option is present, -O sets the value of that option;  +O  unsets
         it.   If shopt_option is not supplied, the names and values of the shell options accepted by shopt are printed on the standard output.  If the invocation option is +O, the output is
         displayed in a format that may be reused as input.
--        A -- signals the end of options and disables further option processing.  Any arguments after the -- are treated as filenames and arguments.  An argument of - is equivalent to --.

Bash also interprets a number of multi-character options.  These options must appear on the command line before the single-character options to be recognized.

--debugger
      Arrange for the debugger profile to be executed before the shell starts.  Turns on extended debugging mode (see the description of the extdebug option to the shopt builtin below).
--dump-po-strings
      Equivalent to -D, but the output is in the GNU gettext po (portable object) file format.
--dump-strings
      Equivalent to -D.
--help Display a usage message on standard output and exit successfully.
--init-file file
--rcfile file
      Execute commands from file instead of the standard personal initialization file ~/.bashrc if the shell is interactive (see INVOCATION below).

--login
      Equivalent to -l.

--noediting
      Do not use the GNU readline library to read command lines when the shell is interactive.

--noprofile
      Do not read either the system-wide startup file /etc/profile or any of the personal initialization files ~/.bash_profile, ~/.bash_login, or ~/.profile.  By default,  bash  reads  these
      files when it is invoked as a login shell (see INVOCATION below).

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

--posix
      Change the behavior of bash where the default operation differs from the POSIX standard to match the standard (posix mode).

--restricted
      The shell becomes restricted (see RESTRICTED SHELL below).

--rpm-requires
      Produce the list of files that are required for the shell script to run.  This implies '-n' and is subject to the same limitations as compile time error checking checking; Command sub‐
      stitutions, Conditional expressions and eval builtin are not parsed so some dependencies may be missed.

--verbose
      Equivalent to  -v.

--version
      Show version information for this instance of bash on the standard output and exit successfully.

bash 命令实例:


启动 bash 交互式 shell 会话:

bash

启动 bash 交互 shell 会话,但加载启动配置:

bash --norc

bash 执行特定的命令:

bash -c "echo 'bash is executed'"

bash 执行特定脚本:

bash path/to/script.sh

bash 执行特定脚本,同时在执行之前打印每个命令:

bash -x path/to/script.sh

bash 执行特定脚本并出现第一个错误的时候停止:

bash -e path/to/script.sh

bash 从 stdin 执行特定命令:

echo "echo 'bash is executed'" | bash

bash 启动受限 shell 会话:

bash -r

bash 扩展阅读:




bash 命令评论