24

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.

jonderry
  • 1,027

3 Answers3

25

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.

bmk
  • 2,195
13

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

Resorath
  • 1,137
3

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}'"'"