I have a simple command line:

uptime | awk '{print $(NF - 2)}'

In a bash shell, this works without a hitch, but when I run it in zsh, I get the following error:

title:5: command not found: NF

As usual this will be a pilot error (me not grokking how expansion works in zsh), but I cannot figure out what to do to get the same command to work in zsh.


The zsh version is 4.3.11 on OSX, and the output from set -xv; uptime | awk '{print $(NF - 2)}' is:

$ set -xv; uptime | awk '{print $(NF - 2)}'
title:5: command not found: NF
+zsh:3> uptime
+zsh:3> awk '{print $(NF - 2)}'
+precmd:1> title '%15<..<%~%<<' '%n@%m: %~'                                                                           
+title:1> [ '' '!=' true ']'
+title:2> [[ xterm-256color == screen* ]]
+title:4> [[ xterm-256color == xterm* ]]
+title:5> print -Pn '\e]2;%n@%m:\ %~\a'
+title:6> print -Pn '\e]1;%15\<..\<%~%\<\<\a'
+zsh:4> git_prompt_info
+git_prompt_info:1> ref=+git_prompt_info:1> ref='' 
+git_prompt_info:1> return
+zsh:4> vi_mode_prompt_info
+vi_mode_prompt_info:1> echo ''

So apparently the awk command works, but my terminal customization is borked.

  • I cannot reproduce this behavior, which version of zsh are you using? Could you post the output of: set -xv; uptime | awk '{print $(NF - 2)}' Commented Sep 13, 2011 at 10:31
  • Could you please update your post and provide the exact command sequence used to reproduce the issue. It seems that the following awk code $( ... ) is interpreted by the shell (as command substitution). I suppose that when you quote the dollar sign \$( ... ) the code will run fine. Commented Sep 13, 2011 at 10:42
  • 1
    Could you try the fork a new instance of zsh with zsh -f and re-run the pipeline? It seems there is a problem with your rc file. Commented Sep 13, 2011 at 10:48
  • Yep, you are right. Thanks for pointing me in the right direction!
    – helgeg
    Commented Sep 13, 2011 at 10:54

1 Answer 1


title:5: command not found: NF

This error message shows an error in a function called title, which by the name presumably sets your terminal's title to the command being run. The subsequent transcript shows title being called by precmd, which is called when a command has finished executing, just before showing the next prompt. But the error is actually triggered by preexec, which is called just before running a command. This function is defined in your ~/.zshrc (or perhaps /etc/zshrc, or in a file that either of them calls).

I can't tell exactly what is wrong without seeing the code, but it looks like the command string is being expanded in some way. Perhaps you have the prompt_subst option set and are printing the command through print -P? You need to escape the command. In particular, do not print it through print -P, print it through print -r and take care of literal control characters. Something like:

print -r ${${${${(qqqq)1}#\$\'}%\'}//\\\'/'}
  • You are right. I used a prepackaged zsh-setup (oh-my-sh) which in one of the imported files defines preexec. I have started from scratch with my setup, free of imported cruft. Zsh is nice, but now I will try to learn from scratch instead of blindly applying a prepackaged setup.
    – helgeg
    Commented Sep 14, 2011 at 9:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .