0

I try to run following command in my terminal:

bash -c 's="test"; s=$(echo "word1  word2 " | awk '{print $1;}'); echo $s;'

It gives me following errors:

bash: -c: line 0: unexpected EOF while looking for matching `)'
bash: -c: line 1: syntax error: unexpected end of file
}); echo $s;: command not found

The problem does not occur when I save my script in a file.

Jakub Balicki
  • 180
  • 1
  • 11

1 Answers1

1

The parameter you are passing to bash with -c uses single quotes. This will "protect" the inner double quotes, but will not work to protect the quotes used to hide '$1' in the awk program.

From bash Man

Enclosing  characters in single quotes preserves the literal value of
each character within the quotes.  A single quote may not occur between 
single quotes, even when preceded by a backslash.

Given that only a single character need to be quoted, Consider using double quotes for the awk program, and escaping the '$'

bash -c 's="test"; s=$(echo "word1  word2 " | awk "{print \$1;}"); echo $s;'

Output:
word1
dash-o
  • 13,723
  • 1
  • 10
  • 37