LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   awk strange behavior in bash (https://www.linuxquestions.org/questions/linux-software-2/awk-strange-behavior-in-bash-670296/)

bingmou 09-16-2008 11:19 AM

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.:)

Uncle_Theodore 09-16-2008 11:26 AM

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?

matthewg42 09-16-2008 11:47 AM

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

ghostdog74 09-16-2008 12:03 PM

to reduce confusion with bash and awk quotes, use -v to pass variables into awk
Code:

var="something"
awk -v var=$var '{...}'


bingmou 09-17-2008 10:51 AM

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?

Uncle_Theodore 09-17-2008 11:20 AM

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.

matthewg42 09-17-2008 12:44 PM

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.

bingmou 09-18-2008 11:27 AM

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 ~~~


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