NachoGomez
NachoGomez

Reputation: 51

Error assingning the output of a command to a variable in bash (Linux)

I wonder why this command:

FILE=`file /usr/bin/java | tr -d \`\' | awk '{print $5}'`

Results in this error message:

bash: command substitution: line 1: unexpected EOF while looking for matching ``'
bash: command substitution: line 2: syntax error: unexpected end of file

If I run the previous command without assigning it to a variable, it works as expected:

$ file /usr/bin/java | tr -d \`\' | awk '{print $5}'
/etc/alternatives/java

Does anyone know why this happens and how can I successfully assign the output value to a variable?

Note: for the curious, I'm trying to find the pointed path to a binary file from a symbolic link, so I can find out if it is a 32 or 64 bits file (in a generic way, not using something like java -version)

Note 2: I've tried removing quotes with sed instead of tr, but it returns the same error

Thank you very much in advance, regards...

Nacho

Upvotes: 0

Views: 86

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

I think it's because you enclosed the commands inside back-ticks. Use $() instead of backticks.

FILE=$(file /usr/bin/java | tr -d \`\' | awk '{print $5}')

Upvotes: 5

Related Questions