Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get watch to work correct with commands that contain quotes, and the watch man page isn't very detailed about how quotes work. To give a concrete example, how can I run the following command inside of watch:

ps -ef | awk -F' ' '{print $2}'

I've tried:

watch "ps -ef | awk -F' ' '{print $2}'"

and

watch 'ps -ef | awk -F\' \' \'{print $2}\''

but neither of these works correctly.

share|improve this question
up vote 9 down vote accepted

I guess you have to escape the $ sign:

watch "ps -ef | awk -F' ' '{print \$2}'"

otherwise it would be interpreted by the shell which would result in an empty string ("") - i.e. awk would print the whole line.

share|improve this answer

You could always put your command in a shell script, then "watch" the script.

share|improve this answer

I just met a similar problem. After reading the watch Man Page, I found a solution that could work, which is to concatenate strings in bash. The final command looked weird, such as:

watch "ps -ef | awk -F' ' '"'{print $2}'"'"

or

watch 'ps -ef | awk -F'"' ' '"'{print $2}'"'"
share|improve this answer

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.