在“ declare -p”输出中,“-”行是什么意思?

时间:2019-05-20 00:00:53

标签: bash declare

编辑:这不是重复的,至少不是针对所引用问题的。这个问题是指使用“-”来停止对命令行中选项的进一步处理。我的问题与内置声明的输出有关。


我不小心declare在脚本中同时包含-l-u的一个变量,但是没有错误。

declare -lu myvar='TestString'
echo "myvar: $myvar"

给哪个myvar: TestString,如果我加上declare -p,我得到一个带有双破折号(--)的声明。

declare -lu myvar='TestString'
declare -p myvar
echo "myvar: $myvar"

哪个给

declare -- myvar="TestString"
myvar: TestString

出于好奇,我从命令行运行了declare -p | grep 'declare --',并得到了很多匹配的变量。有些来自我的设置,但有些来自bash本身。

declare -- BASH="/bin/bash"
declare -- BASH_ARGV0
declare -- BASH_COMMAND
declare -- BASH_SUBSHELL
declare -- BASH_VERSION="5.0.7(1)-release"
declare -- COLUMNS="211"
declare -- COMPLETION_UTIL=" pmbin pmcat pmchkver pmcore pmcost pmdoc pmedit pminfo pmless pmlines pmlist pmman pmpath pmstripper pmuninst pmversion pmxs podpath"
declare -- COMP_WORDBREAKS
declare -- CU="podpath"
declare -- DZIL=""
declare -- EPOCHREALTIME
declare -- EPOCHSECONDS
declare -- HISTCONTROL="erasedups:ignoreboth"
declare -- HISTFILE="/home/harleypig/.bash_history"
declare -- HISTFILESIZE="100000"
declare -- HISTIGNORE="&:[ ]*:exit:ls:bg:fg:history:clear"
declare -- HISTSIZE="500000"
declare -- HISTTIMEFORMAT="%F %T "
declare -- HOSTNAME="sweetums"
declare -- HOSTTYPE="x86_64"
declare -- IFS=" 
declare -- LINES="55"
declare -- MACHTYPE="x86_64-pc-linux-gnu"
declare -- OPTERR="1"
declare -- OSTYPE="linux-gnu"
declare -- POD=""
declare -- PROMPT_COMMAND="_update_prompt"
declare -- PS1="\\[\\001\\002\\001\\002\\]\\n\\[\\001\\002\\001\\002\\]REBOOT - Current: 5.0.13-arch1-1-ARCH Latest: 5.1.2-arch1-1-ARCH\\nPacman Last Updated: 23h 20m\\n\\001\\002\\001\\002\\u\\[\\001\\002\\001\\002\\]@\\001\\002\\001\\002\\h\\[\\001\\002\\001\\002\\]: /home/harleypig\\[\\001\\002\\001\\002\\]\\n\\[\\001\\002\\001\\002\\001\\002:)\\001\\002\\001\\002\\] \$ "
declare -- PS2="> "
declare -- PS4="+ "
declare -- SECONDS
declare -- _="vim"
declare -- _backup_glob="@(#*#|*@(~|.@(bak|orig|rej|swp|dpkg*|rpm@(orig|new|save))))"
declare -- circled_digits="⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳"
declare -- color_off="\\[\\001\\002\\001\\002\\]"
declare -- exit_status="\\[\\001\\002\\001\\002\\001\\002:)\\001\\002\\001\\002\\]"
declare -- filename="/tmp/bash-template-D8fdfMZ"
declare -- hostname="\\001\\002\\001\\002\\h\\[\\001\\002\\001\\002\\]"
declare -- lesspipe=""
declare -- nvm_init="/usr/share/nvm/init-nvm.sh"
declare -- pacman_status="Pacman Last Updated: 23h 20m"
declare -- prompt_color="\\[\\001\\002\\001\\002\\]"
declare -- system_reboot="REBOOT - Current: 5.0.13-arch1-1-ARCH Latest: 5.1.2-arch1-1-ARCH"
declare -- user="\\001\\002\\001\\002\\u\\[\\001\\002\\001\\002\\]"
declare -- vim_bundles=".vim/bundle"

那么,声明输出中的'-'是什么意思?

我已经阅读了很多bash手册,但是我看不出这个解释可能在哪里。

1 个答案:

答案 0 :(得分:0)

man 1 bash

declare [-aAfFgilnrtux] [-p] [name[=value] ...]
...
When -p is supplied without name arguments, it will display the 
attributes and values of all variables having the attributes specified 
by the additional options. If no other options are supplied with -p, 
declare will display the attributes and values of all shell variables. 
...

您的问题是“我运行了declare -p | grep 'declare --' ...”,它将返回每个普通shell变量声明的开始行,因为它们在没有"declare -- ..."本身的情况下被格式化为declare -p任何特别声明的内容,例如-i等...