I have these file names that literally have double quotes in the path to deal with special characters issues, I want to loop through and echo the file paths while preserving the quotes, this seems to remove them:
for value in temp/sample."sample.id1".genotypes.txt temp/sample."sample.id2".genotypes.txt; do echo $value; done
I tried this but no luck:
for value in temp/sample."sample.id1".genotypes.txt temp/sample."sample.id2".genotypes.txt; do echo '${value}'; done
How do I do this?
CodePudding user response:
You need to quote the strings to preserve the double quotes:
for value in 'temp/sample."sample.id1".genotypes.txt' 'temp/sample."sample.id2".genotypes.txt'; do
echo $value
done
Otherwise, writing some."thing" is identical to some.thing because the shell interprets the quotes.
CodePudding user response:
for things like this, I like to use a slightly different approach that looks like a better design to me:
# make an array with the data
mapfile -t ary <<"EOF"
temp/sample."sample.id1".genotypes.txt
temp/sample."sample.id2".genotypes.txt
EOF
# use the data from the array
for f in "${ary[@]}"; do
printf '%s\n' "$f"
done
It will make your life a bit easier if your data grows, and you can then very easily transfer it to another file. Of course, if it's only for a one-time use (e.g., you made an error when naming your files and you only want to rename them), then learn how to have Bash properly parse the quotes as shown in the other answers (escaping them or using single quotes).
CodePudding user response:
You can also escape it :
for value in temp/sample.\"sample.id1\".genotypes.txt temp/sample.\"sample.id2\".genotypes.txt; do echo $value; done
