bash - 表示您是否是 root 的 $ 或 # 符号的名称是什么?

标签 bash shell

我最近发现 # 符号表示您是 shell 的根用户(至少在 bash shell 中),而 $ 符号表示你不是。

这个标志的名称是什么,它真的具有我赋予它的含义吗?

它只是一个 bash 东西吗?

Example with default bash configuration on Ubuntu:

john@mycomputer /tmp $ echo "I'm a simple user"
mycomputer /tmp # echo "I'm the root user"

最佳答案

表明您的权限的提示字符似乎没有正式定义。

thorough answer to “What is the origin of the UNIX $ (dollar) prompt?”提供了很多历史背景,但它也没有这个指标的名称。

在没有官方术语的情况下,我将其称为“提示特权指示符”或“提示指示符符号”,甚至简称为“提示指示符”。

所有 shell 都有一个提示符,其默认字符串以指示符号结尾,(默认情况下)几乎总是指示您是否是 root。大多数 shell 允许您更改提示,并且大多数 shell 都有指定提示指示符的方法。

以下是您可以在各种 shell 文档和标准中找到的内容:

bash man page 的一个片段:

PROMPTING
      When executing interactively, bash displays the primary prompt PS1 when
      it is ready to read a command, and the secondary  prompt  PS2  when  it
      needs  more  input  to  complete a command.  Bash displays PS0 after it
      reads a command but before executing  it.   Bash  allows  these  prompt
      strings  to  be  customized  by inserting a number of backslash-escaped
      special characters that are decoded as follows:
…
             \$     if the effective UID is 0, a #, otherwise a $

来自zsh man pages :

Shell state
    %#       A `#' if the shell is running with privileges,  a  `%'  if  not.
             Equivalent  to `%(!.#.%%)'.  The definition of `privileged', for
             these purposes, is that either the effective user  ID  is  zero,
             or,  if  POSIX.1e  capabilities are supported, that at least one
             capability is raised in  either  the  Effective  or  Inheritable
             capability vectors.

POSIX standard甚至没有指定特权提示符号应该是什么:

PS1

Each time an interactive shell is ready to read a command, the value of this variable shall be subjected to parameter expansion and written to standard error. The default value shall be $ . For users who have specific additional implementation-defined privileges, the default may be another, implementation-defined value.

另见 dash man page (dash 是一个流行的基本 POSIX shell,often 安装为/bin/sh):

    PS1      The primary prompt string, which defaults to ``$ '', unless you
             are the superuser, in which case it defaults to ``# ''.

POSIX 和破折号在您可以将 $PS1 设置为的内容中没有任何动态。默认提示被设置一次,并被对 PS1 变量的任何赋值所取代,失去了使用单个字符区分 root 用户和非特权用户的能力。您必须编写自己的代码来确定您是否有特权,并在您的 PS1 作业中使用它的输出。

关于bash - 表示您是否是 root 的 $ 或 # 符号的名称是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41930997/

相关文章:

shell脚本和C之间的通信

linux - 如何从 bash 脚本启动程序,然后关闭终端但让程序保持事件状态

linux - 通过本地重定向从本地脚本远程 sudo

ruby - 检测是否安装了 git 的独立于平台的方法

bash - 如何获取 Bash 命令历史统计信息,例如最近 6 个月中最常用的命令?

bash - bash 中前导零的范围

bash - 如何获取当前shell脚本的完整路径名?

linux - bash 4.4 不将变量传递给子 shell

bash - 提取文件名 shell 脚本的一部分

linux - 从另一个具有动态路径的 bash 脚本调用 bash 脚本