Is there a way of having the command I just entered echoed onto the screen after entering?
Ex:
$ echo hello + echo hello hello I know this can be done with bash -x but I could not find the equivalent in the zsh manual.
The -x (or -o xtrace) option works in zsh too. That comes from the Bourne shell in the late 70s and is supported by all Bourne like shells. From 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).
Example:
#!/bin/zsh -x echo hello and an example run:
$ /tmp/ex.sh +/tmp/ex.sh:3> echo hello hello Like in bash / ksh, it can be enabled with set -x or set -o xtrace and disabled afterwards with set +x or set +o xtrace. It's also possible to enable tracing on a per-function basis with functions -t myfunction.
Beware that in interactive shells, if you've enabled a number of fancy plugins or advanced completion, then you'll also see the tracing corresponding to the execution of those which may affect your interactive shell experience.
setopt XTRACE or setopt -x in .zshrc? Addendum the correct answer by Andy Dalton, relative to comment by Will …
I tried it but it caused my terminal to output a bunch of random stuff so I assumed it wasn't right.
For zsh, add-zsh-hook -d precmd update_terminal_cwd can be used reduce trace XTRACE clutter in Apple's Terminal app.
TL;DR
In the case of Apple's Terminal app, there is an addition of update_terminal_cwd() which runs at each prompt update.
The update_terminal_cwd call is also shown in, and adds clutter to, the '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}" Bash workaround: unset PROMPT_COMMAND or modify PROMPT_COMMAND to not use 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 Zsh workaround can be done with a -d delete from the 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 )
zsh -x?