From 8ad15bde9faf451883e512cd358c5e92c6335af5 Mon Sep 17 00:00:00 2001 From: Alexander Schlarb Date: Sun, 16 May 2021 19:54:39 +0200 Subject: [PATCH] Fix running in shells with `-u`/`-o nounset` option set --- z.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/z.sh b/z.sh index 13008a6..4a93a5f 100644 --- a/z.sh +++ b/z.sh @@ -39,7 +39,7 @@ _z() { [ -h "$datafile" ] && datafile=$(readlink "$datafile") # bail if we don't own ~/.z and $_Z_OWNER not set - [ -z "$_Z_OWNER" -a -f "$datafile" -a ! -O "$datafile" ] && return + [ -z "${_Z_OWNER+set}" -a -f "$datafile" -a ! -O "$datafile" ] && return _z_dirs () { local line @@ -93,7 +93,7 @@ _z() { if [ $? -ne 0 -a -f "$datafile" ]; then env rm -f "$tempfile" else - [ "$_Z_OWNER" ] && chown $_Z_OWNER:"$(id -ng $_Z_OWNER)" "$tempfile" + [ "${_Z_OWNER+set}" ] && chown "$_Z_OWNER:$(id -ng "$_Z_OWNER")" "$tempfile" env mv -f "$tempfile" "$datafile" || env rm -f "$tempfile" fi @@ -222,13 +222,14 @@ _z() { alias ${_Z_CMD:-z}='_z 2>&1' -[ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P" +[ "${_Z_NO_RESOLVE_SYMLINKS+set}" ] || _Z_RESOLVE_SYMLINKS="-P" if type compctl >/dev/null 2>&1; then # zsh - [ "$_Z_NO_PROMPT_COMMAND" ] || { + typeset -a _Z_EXCLUDE_DIRS + [ "${_Z_NO_PROMPT_COMMAND+set}" ] || { # populate directory list, avoid clobbering any other precmds. - if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then + if [ "${_Z_NO_RESOLVE_SYMLINKS+set}" ]; then _z_precmd() { (_z --add "${PWD:a}" &) : $RANDOM @@ -252,9 +253,10 @@ if type compctl >/dev/null 2>&1; then compctl -U -K _z_zsh_tab_completion _z elif type complete >/dev/null 2>&1; then # bash + typeset -a _Z_EXCLUDE_DIRS # tab completion complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z} - [ "$_Z_NO_PROMPT_COMMAND" ] || { + [ "${_Z_NO_PROMPT_COMMAND+set}" ] || { # populate directory list. avoid clobbering other PROMPT_COMMANDs. grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || { PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''(_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null &);'