Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

[Edit]

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)}'
0.51
+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.

share|improve this question
    
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)}' –  Dimitre Radoulov Sep 13 '11 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. –  Dimitre Radoulov Sep 13 '11 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. –  Dimitre Radoulov Sep 13 '11 at 10:48
    
Yep, you are right. Thanks for pointing me in the right direction! –  helgeg Sep 13 '11 at 10:54
add comment

migrated from stackoverflow.com Sep 14 '11 at 9:59

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 5 down vote accepted

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}#\$\'}%\'}//\\\'/'}
share|improve this answer
    
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 Sep 14 '11 at 9:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.