Reputation: 469
I tried this but it does not seem to work. Please help thanks
TEST_STRING= test
echo Starting awk command
awk -v testString=$TEST_STRING'
BEGIN {
}
{
print testString
}
END {}
' file2
Upvotes: 0
Views: 116
Reputation: 295687
There are two problems here: You aren't actually assigning to TEST_STRING
, and you're passing the program code in the same argument as the variable value. Both of these are caused by whitespace and quoting being in the wrong places.
TEST_STRING= test
...does not assign a value to TEST_STRING
. Instead, it runs the command test
, with an environment variable named TEST_STRING
set to an empty value.
Perhaps instead you want
TEST_STRING=test
or
TEST_STRING=' test'
...if the whitespace is intentional.
Second, passing a variable to awk with -v
, the right-hand side should be double-quoted, and there must be unquoted whitespace between that value and the program to be passed to awk (or other values). That is to say:
awk -v testString=$TEST_STRING' BEGIN
...will, if TEST_STRING
contains no whitespace, pass the BEGIN
as part of the value of testString
, not as a separate argument!
awk -v testString="$TEST_STRING" 'BEGIN
...on, the other hand, ensures that the value of TEST_STRING
is passed as part of the same argument as testString=
, even if it contains whitespace -- and ensures that the BEGIN
is passed as part of a separate argument.
Upvotes: 2