This question already has an answer here:

I have a CT.txt with below input data. CT.txt:

test testing test 1

And with below script:

for CITY in $(cat /home/user/CT.txt)
echo $FILES

Giving me output as follow:


where output should be:

/mnt/dir1/dir2/test 1

How to make CT.txt file to read having space in between test 1.

marked as duplicate by Gilles bash Aug 3 '17 at 22:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • if your CT.txt content is linear you cannot even achieve it by quoting. Is your CT.txt content are in one line only? – αғsнιη Aug 3 '17 at 8:22

If you change the FS (field seperator) in awk you could achieve it.

awk -F: '{ print "/mnt/dir1/dir2/"$1 }' CT.txt
  • The CT.txt there is no field seperator :! also all contents are in one line but you just reading one, even if it was space or tab, we don't know test 1 is filename or test and 1 are seperate files – αғsнιη Aug 3 '17 at 8:37
  • Yes, if all is in one line, it wouldn't work, but if each line is an entry, than we could achieve it with awk. – Vafa Aug 3 '17 at 10:38

Assuming the CT.txt has entries one / line, then the while loop is better suited to your task:

while IFS= read -r CITY; do
   echo "$FILES"/*
done < /home/user/CT.txt

The for loop is ill-suited for this, however, it can still be made to work:

# set the input field separator to newline and disable globbing
IFS=$'\n';set -f
for CITY in $(< /home/user/CT.txt); do
   echo "$FILES"/*

Not the answer you're looking for? Browse other questions tagged or ask your own question.