tags:

views:

341

answers:

4

Hello,

To illustrate my problem,

TEST="Hi my name is John"
OUTP=`echo $TEST | awk '{print $3}'`
echo $OUTP

What I would expect this to do is pass the $TEST variable into awk and store the 3rd word into $OUTP.

Instead I get "Hi: not found", as if it is expecting the input to be a file. If I pass just a string instead of a variable, however, there is no problem. What would be the best way to approach this?

Thanks all!

+2  A: 

Your code works for me, as-is.

[bloom@little-cat-a ~]$ TEST="Hi my name is John"
[bloom@little-cat-a ~]$ OUTP=`echo $TEST | awk '{print $3}'`
[bloom@little-cat-a ~]$ echo $OUTP
name
Ken Bloom
+1  A: 
#!/bin/bash
TEST="Hi my name is John"
set -- $TEST
echo $3

#!/bin/bash
TEST="Hi my name is John"
var=$(echo $TEST|awk '{print $3}')
echo $var
nice use of `set --`
glenn jackman
This seems to work in the command prompt, but when run as a shell script I still get Hi: not found. Any way to fix this?Thank you!
Andrew Smith
EDIT: Nevermind, I was putting an extra space before the $ when setting the variable. Thank you very much for the help!
Andrew Smith
+1  A: 

As with others, this works for me as-is, but perhaps adding double-quotes (") around $TEST in line 2 would help. If not, more specific information about the system on which you are running bash might help.

Isaac
A: 

One way to reproduce similar behavior:

$ alias echo='echo;'
$ echo Hi
Hi: command not found
$ alias
alias echo='echo;'
$ unalias echo
$ echo Hi
Hi
Dennis Williamson