
¿Hay alguna manera de que el comando que acabo de ingresar se repita en la pantalla después de ingresar?
Ex:
$ echo hello
+ echo hello
hello
Sé que esto se puede hacer, bash -x
pero no pude encontrar el equivalente en el manual de zsh.
Respuesta1
La opción -x
(o -o xtrace
) también funciona zsh
. Eso proviene del shell Bourne a finales de los 70 y es compatible con todos los shells similares a Bourne. De man zshoptions
/ info zsh xtrace
:
XTRACE (-x, ksh: -x) Print commands and their arguments as they are executed. The output is preceded by the value of $PS4, formatted as described in the section EXPANSION OF PROMPT SEQUENCES in zshmisc(1).
Ejemplo:
#!/bin/zsh -x
echo hello
y un ejemplo ejecutado:
$ /tmp/ex.sh
+/tmp/ex.sh:3> echo hello
hello
Al igual que en bash
/ ksh
, se puede habilitar con set -x
o set -o xtrace
y luego deshabilitarlo con set +x
o set +o xtrace
. También es posible habilitar el seguimiento por función con functions -t myfunction
.
Tenga en cuenta que en los shells interactivos, si ha habilitado varios complementos sofisticados o la finalización avanzada, también verá el seguimiento correspondiente a la ejecución de aquellos que pueden afectar su experiencia del shell interactivo.
Respuesta2
Anexo la respuesta correcta de Andy Dalton, en relación con el comentario de Will...
Lo intenté pero provocó que mi terminal generara un montón de cosas aleatorias, así que supuse que no estaba bien.
Para zsh, add-zsh-hook -d precmd update_terminal_cwd
se puede utilizar para reducir XTRACE
el desorden de rastros en la aplicación Terminal de Apple.
TL;DR
En el caso de la aplicación Terminal de Apple, hay una adición que update_terminal_cwd()
se ejecuta en cada actualización.
La update_terminal_cwd
llamada también se muestra y agrega desorden al 'set -x' XTRACE
.
username@hostname ~ % echo hello
# +-zsh:2> echo hello
# hello
# +update_terminal_cwd:5> local url_path=''
# +update_terminal_cwd:10> local i ch hexch LC_CTYPE=C LC_COLLATE=C LC_ALL='' LANG=''
# +update_terminal_cwd:11> i = 1
#
# … <snip>
#
# +update_terminal_cwd:22> printf '\e]7;%s\a' #file://hostname.local/Users/username
/etc/bashrc_Apple_Terminal
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL, including
# the host name to disambiguate local vs. remote paths.
# … <snip>
printf '\e]7;%s\a' "file://$HOSTNAME$url_path"
}
PROMPT_COMMAND="update_terminal_cwd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
Solución alternativa de Bash: unset PROMPT_COMMAND
o modificar PROMPT_COMMAND
para no utilizar update_terminal_cwd
.
/etc/zhrc_Apple_Terminal
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL, including
# the host name to disambiguate local vs. remote paths.
# Percent-encode the pathname.
local url_path=''
{
# … <snip>
}
printf '\e]7;%s\a' "file://$HOST$url_path"
}
# Register the function so it is called at each prompt.
autoload -Uz add-zsh-hook
add-zsh-hook precmd update_terminal_cwd
La solución alternativa a Zsh se puede realizar eliminando -d
del precmd
zsh-hook:
### `-L` list
user@host ~ % add-zsh-hook -L
# typeset -g -a zshexit_functions=( shell_session_update )
# typeset -g -a precmd_functions=( update_terminal_cwd )
user@host ~ % add-zsh-hook -d precmd update_terminal_cwd
user@host ~ % add-zsh-hook -L
# typeset -g -a zshexit_functions=( shell_session_update )
user@host ~ % set -x
user@host ~ % echo hello
# +-zsh:8> echo hello
# hello
user@host ~ % set +x; add-zsh-hook -L
# typeset -g -a zshexit_functions=( shell_session_update )