How to extract a value (in a specific row and column) from a file and assign it to a new variable

To store the output of a command in a variable, use :

variable=$( commandFooBar )

Check HERE


You are mixing up two different shell operators.

The > redirect, will redirect output to a file. So to get your example to work, you could use:

awk 'NR == 2 {print $3}' a.txt > price
cat price   # display contents of file named price

The $ refers to a variable, so to get your echo to print out the variable, you would first have to set it from the output of the previous. Like so:

price=$(awk 'NR == 2 {print $3}' a.txt)
echo "$price"

Tags:

Linux