Saturday 13 April 2019

bash - Using the watch command with an argument that contains quotes


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.



Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...