Verwirrt, weil echo "PATH=$PATH:/usr/local/sbin"
das nicht der Fall ist (dachte, es hätte etwas mit zu tun :
).
Auch in Bash funktionieren beide Befehle wie erwartet.
$ 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
Antwort1
Denn das :s
Nachher $USER
wird interpretiert alsErweiterungsmodifikator. Dies können Sie deutlich erkennen, wenn Sie Folgendes tun:
% 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
Und wie Sie der obigen Liste entnehmen können, :/
handelt es sich nicht um einen Erweiterungsmodifikator.
${PATH}
Ist es dann empfehlenswert, immer ,${USER}
, usw. zu verwenden ?
Nein, normalerweise reicht es aus, einfach zu verwenden $USER
, aberManchmal,wie Sie sehen, ist die Verwendung erforderlich ${USER}
. :)
Bezüglich des Codes in deiner Frage kann ich dir jedoch zwei weitere Empfehlungen zur Verwendung in Zsh geben:
- Verwenden
$path
anstatt$PATH
Und - verwenden
print
anstattecho
.
% 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