echo “$USER:staff” が zsh: bad replacement をスローするのはなぜですか?

echo “$USER:staff” が zsh: bad replacement をスローするのはなぜですか?

ないので困惑していますecho "PATH=$PATH:/usr/local/sbin"( と関係があると思っていました:)。

また、Bash では、両方のコマンドが期待どおりに動作します。

$ echo "PATH=$PATH"
PATH=/usr/local/bin

$ echo "PATH=$PATH:/usr/local/sbin"
PATH=/usr/local/bin:/usr/local/sbin

$ echo "$USER:staff"
zsh: bad substitution

答え1

after:s$USER次のように解釈されるので拡張修飾子次の操作を行うと、これが明確にわかります。

% autoload -Uz compinit; compinit       # Init completion system
% zstyle ':completion:*' group-name ''  # Enable completion grouping
% zstyle ':completion:*' format '%d'    # Add titles to the groups
% print $USER: # and press Tab or ^D right after the `:`
modifier
&  -- repeat substitution
A  -- as ':a', then resolve symlinks
P  -- realpath, resolve '..' physically
Q  -- strip quotes
a  -- absolute path, resolve '..' lexically
c  -- PATH search for command
e  -- leave only extension
g  -- globally apply s or &
h  -- head - strip trailing path element
l  -- lower case all words
q  -- quote to escape further substitutions
r  -- root - strip suffix
s  -- substitute string
t  -- tail - strip directories
u  -- upper case all words

上記のリストからわかるように、:/これは拡張修飾子ではありません。

では、常に、、などを使用することをお勧めします${PATH}${USER}?

いいえ、通常は だけ使用しても問題ありません$USERが、時々、ご覧のとおり、 を使用する必要があります${USER}。:)

ただし、質問のコードに関しては、Zsh で使用するための他の 2 つの推奨事項を提供できます。

% print $PATH
/usr/local/bin

% print $path
/usr/local/bin

% path+=/usr/local/sbin  # $path is an array, not a string

% print $PATH            # $path and $PATH are "tied" & automatically in sync
/usr/local/bin:/usr/local/sbin

% print -c $path         # Print the items in columns, like `ls`
/usr/local/bin   /usr/local/sbin

% print -l $path         # Print one item per line, like `ls -l`
/usr/local/bin
/usr/local/sbin

% path+=/usr/local/sbin

% print -c $path       
/usr/local/bin   /usr/local/sbin  /usr/local/sbin

% typeset -U PATH path   # Make each item unique/Eliminate duplicates

% print -c $path       
/usr/local/bin   /usr/local/sbin

関連情報