user3570398
user3570398

Reputation: 345

Bash: replacement of string with quotes (bad substitution error)

I am trying to modify lines of a file through a bash script (version 3.2) and have tried many options, but they all give a 'bad substitution error'.

For example, I extract the line from $filename (previously defined variable)

line=$(sed -n 24p $filename)

echo ${$line/coordinateIndex="0"/coordinateIndex="124"} 

I am trying to replace the coordinateIndex to some other numbers. Those numbers are between "" in the file and I need to keep that format.

Any help appreciated; thank you!

Upvotes: 1

Views: 1721

Answers (1)

konsolebox
konsolebox

Reputation: 75458

echo ${$line/coordinateIndex="0"/coordinateIndex="124"} 

should not have the $ inside:

echo ${line/coordinateIndex="0"/coordinateIndex="124"}

You may also need to quote it properly to properly match the double-quotes.

echo ${line/coordinateIndex=\"0\"/coordinateIndex="124"}

And it's also better to enclose your argument around double-quotes to prevent word splitting with values of IFS and unexpected pathname expansion:

echo "${line/coordinateIndex=\"0\"/coordinateIndex="124"}"

See more detail about usage of Parameter Expansion in the Bash Manual.

Upvotes: 2

Related Questions