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.:) |
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? |
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
You can overcome the problem like this: Code:
awk "BEGIN { print \"$var\" }" |
to reduce confusion with bash and awk quotes, use -v to pass variables into awk
Code:
var="something" |
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? |
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. |
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. |
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. |