Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-12-2010, 11:35 AM   #1
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
How to 'watch' a command including both of single and double quote?

Hi guys,

I want to watch a number of processes in "D" status repeatedly with following command:
# watch -n 1 'top -b -n 1 | awk '{if ($8 == "D") {print; count++} } END {print count}''
but it didn't work. I also tried with double quote.

Can I use 'watch' command is this case?

PS: I know 'while' and 'sleep' can do the same but it is dirty workaround.
Old 08-12-2010, 11:45 AM   #2
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,823

Rep: Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611
1. You can't do quotes like that. Either escape them or stagger them. ' then " then ', then close them in reverse order. You have ' then ' then " and it sees 'top -b -n 1 | awk ' as one unit.
2. $8 needs to be escaped (\$8) so that the shell doesn't parse it.

watch -n 1 "top -b -n 1 | awk '{if (\$8 == "D") {print; count++} } END {print count}'"
works without error, though it prints just "2" for me and I don't know why.
Old 08-12-2010, 02:29 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
You also have to escape the double-quotes inside the awk command.
watch -n 1 "top -b -n 1 | awk '{if (\$8 == \"D\") {print; count++} } END {print count}'"
Basically, single-quotes escape everything, while double-quotes escape everything inside them except for $, `, and \, and of course another ", which ends the sequence. This means that inside double-quotes you can use \ to escape anything that could be a problem, including other double-quotes. But inside single-quotes you can't use anything, not even the escape character.

Note also that double-quotes lose their special meaning inside single quotes, and vice-versa.

So in this case, you have to use double-quotes around the phrase as a whole, which allows you to use escapes, then you need to escape any [$`\"] characters that are intended to be fed to the commands inside. In this case, you need to protect the $ and " inside the awk expression, because the single-quotes surrounding them have lost their special bash meaning.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed : Convert double quote to singe quote pgb_710 Programming 6 01-21-2010 07:59 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
gvim - double quote char always replaced with two single quotes in special modes JazzItSelf Linux - Software 4 09-16-2008 11:26 AM
minicom won't send a double quote character kscott121 Linux - Software 0 05-22-2008 02:34 PM
Double and single quote, Question in expect evilchild Programming 4 08-22-2004 05:34 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:37 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration