简体   繁体   中英

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM