简体   繁体   中英

Bash: how to use variable value as substring of a folder name

In a bash script, I don't remember how to use the value of a variable as a substring of a folder name. Example:

I have folders:

test_1.0_xxx  test_1.2_yyy test_1.4_zzz

And I want to do the following:

for i in 1.0 1.2 1.4
do
  cd test_$i_*
  # do something
done

The syntax seems to be wrong:

bash: cd: test_*: No such file or directory

I also tried using backticks around $i , but with no success. How should I do it?

Try with this:

#!/bin/bash
for i in "1.0" "1.2" "1.4"
do
  cd test_${i}_*
  # do something
done

The underscore following $i is being counted as part of the variable name. Try enclosing it in quotes:

for i in 1.0 1.2 1.4
do
  cd test_"$i"_*
  # do something
done

Edit : matteomattei's version is probably more correct.

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