LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-16-2008, 12:19 PM   #1
bingmou
Member
 
Registered: Sep 2008
Location: Beijing
Distribution: GNU/Linux Debian
Posts: 32

Rep: Reputation: 0
Question awk strange behavior in bash


I use awk to print something and I get a error message :
awk: line 1: runaway string constant "like ...

The detail input is below:

i@ubuntu:~$ var="like"
i@ubuntu:~$ awk 'BEGIN{print "'$var'"}'
like
i@ubuntu:~$ var="like you"
i@ubuntu:~$ awk 'BEGIN{print "'$var'"}'
awk: line 1: runaway string constant "like ...

I google the error message and find some guys met it before and just gave a solution (to use option -v),and no one told what is wrong with awk. I just want to know why awk goes wrong when $var has space in it .
Thank you !
or you can tell me which part of manual should I read.
 
Old 09-16-2008, 12:26 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
I can't reproduce your bug. Here's what I get

teddy@office ~$ awk 'BEGIN{$var="like you"; print $var}'
like you

teddy@office ~$ var="like you"
teddy@office ~$ awk 'BEGIN{print ""$var""}'

teddy@office

Did you type your commands exactly as you input them?
 
Old 09-16-2008, 12:47 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I can re-produce your problem. It is because of the quoting.

Here's how bash unwraps your quotes ($ being the prompt):
Code:
$ set -vx
$ awk 'BEGIN{print "'$var'"}'
awk 'BEGIN{print "'$var'"}'
+ awk 'BEGIN{print "like' 'you"}'
awk: BEGIN{print "like
awk:             ^ unterminated string
The part between the first two single quotes is interpreted as a literal string. The variable value is then put in the middle, but it contains a space. This means that after parsing the quotes, bash sees the command as awk, and the argument list as:
  1. BEGIN{print "like
  2. you"}
awk's syntax is that the first parameter is a program, which is invalid, due to the unterminated string as described in the error message.

You can overcome the problem like this:
Code:
awk "BEGIN { print \"$var\" }"

Last edited by matthewg42; 09-16-2008 at 12:48 PM.
 
Old 09-16-2008, 01:03 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
to reduce confusion with bash and awk quotes, use -v to pass variables into awk
Code:
var="something"
awk -v var=$var '{...}'
 
Old 09-17-2008, 11:51 AM   #5
bingmou
Member
 
Registered: Sep 2008
Location: Beijing
Distribution: GNU/Linux Debian
Posts: 32

Original Poster
Rep: Reputation: 0
Thank you very much Uncle,_Theodore,Matthew Gates and ghostdog74!

It is all about the quotes:single quotes, double quotes, and reverse quote. I am quite confused about quotes .Although I have read some article about the quotes in linux , but I still can't catch it and make some mistakes. Maybe I should learn it again more carefully.

Uncle,_Theodor , in your case , you use double quotes to wrap the awk command and you won't make the some mistake I made.

BTW, can anybody tell me the meaning of "set -vx ". I can't find the manual of "set". Does that command set some environment variables ? How to set it back?
 
Old 09-17-2008, 12:20 PM   #6
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
set is a bash builtin command. You can read its description in the bash manual. In man bash it's somewhere around lines 4200 and down. Here's what it says about -v and -x

-v Print shell input lines as they are read.
-x After expanding each simple command, for command, case
command, select command, or arithmetic for command, dis-
play the expanded value of PS4, followed by the command
and its expanded arguments or associated word list.
 
Old 09-17-2008, 01:44 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Single quotes: Everything between then is a literal string. No variable values are substituted, no backtick command expansion is done, escaping characters has no effect (even \ is treated as a literal). Putting a single quote within single quotes is not possible.

Double quotes: variables and backtick command substitution is done when the quoted section is evaluated. You can escape some characters by putting a backslash in front of them. E.g. if th variable var has the value "bananas" "hello $var" will expand to "hello bananas", but "hello \$var" will expand to the literal string "hello $var" - the $ is turned into a literal character by prefixing with a backslash.

The order of interpolation (and other information I didn't cover here) is explained in the QUOTING section of the bash manual page.
 
Old 09-18-2008, 12:27 PM   #8
bingmou
Member
 
Registered: Sep 2008
Location: Beijing
Distribution: GNU/Linux Debian
Posts: 32

Original Poster
Rep: Reputation: 0
Thank you ,everyone ! I will read the quoting part of the manual of Bash and I hope I can get some helpful information , to make less mistakes ~~~

Last edited by bingmou; 09-18-2008 at 12:29 PM.
 
  


Reply


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
Very Strange Behavior raysr Mandriva 4 08-31-2004 03:06 PM
strange sed/bash behavior mpdavig Programming 1 07-24-2004 03:27 AM
Strange Behavior andrewb758 Linux - Hardware 5 08-31-2003 03:42 PM
strange behavior abhijit Linux - General 3 07-10-2003 12:25 AM
Strange Sound Behavior Benkong2 Linux - Newbie 0 12-29-2002 11:12 AM


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

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