Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

As expected this prints every line of the 'testEmpty' file to the console.

while read i; do
    echo $i
done < testEmpty

....but this:

while read i; do
    name=$i
    echo $name
done < testEmpty

only prints out blanks lines.

The reason I am doing this is because on each line of the 'testEmpty' file there is a URL which I am feeding to curl within this same while loop (removed for clarity) and I wish to append a file extension to part of the url to serve as a filename and I thought it would be quite neat to do this in the same loop.

BTW The 'testEmpty' file should only ever really have only 1 or maybe 2 entries so I'm not bothered about overhead etc. which I believe could well be an issue if I were processing huge files, so I have no need to worry about that.

Thank you

share|improve this question
    
please input your sample file and desire output –  Rahul Patil Mar 28 at 16:19
    
Did you try "while read i; do name=$i; echo $i; done < testEmpty"? –  lanes Mar 28 at 16:26
2  
@user1587462, actually, using sed isn't typically the right way to do an alteration; bash has native string-manipulation functionality built in. For instance, name=${i//$foo/$bar} will save the expanded contents of $i to $name, replacing $foo with $bar. –  Charles Duffy Mar 28 at 16:47
1  
@user1587462 ...see mywiki.wooledge.org/BashFAQ/100 for general information on string manipulation in bash, or mywiki.wooledge.org/BashFAQ/073 for a shorter description focused on parameter expansion. –  Charles Duffy Mar 28 at 16:48
1  
@CharlesDuffy Thanks. Using the native string manipulation of bash instead of sed worked a treat. The website you suggested was excellent also. –  user1587462 Mar 29 at 13:55

2 Answers 2

@CharlesDuffy had it when he suggested that using sed was not the right way to do string manipulation. The simple answer to the question is this:

while read i; do
    name=${i}
    echo $name
done < testEmpty
share|improve this answer

This form

name=$i echo $name

will substitute the variables before launching the command, so when echo actually runs, bash sees

name="contents of the line" echo 

Also, you won't get the value of the previous line because var=value cmd ... only sets the var variable in the environment of the command for the duration of the command.

You'll need to ensure the variable is set before you want it dereferenced:

name=$i; echo $name
# or if you're launching some other command, protect the variable:
name=$i sh -c 'echo $name'
share|improve this answer
    
The assignment was on a separate line; the lack of indentation in the original post hid that. –  chepner Mar 28 at 17:17
    
@chepner it was, I hadn't realised when I posted. Apologies for that. –  user1587462 Mar 28 at 17:54
    
Oh. Well in that case, apart from the lack of quotes, I don't understand why it "didn't work". –  glenn jackman Mar 28 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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