[SOLVED] foo1=$(cat foo2) does not work as expected (by me).
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why does not the whole output of 'cat radios' go into u12? I suspect it has something to do with the newline char, but do not exactly understand why it fails. Thanks.
I suspect this is because you store it in a variable. So this is translated like this:
Code:
u12=Antena 2: mms://195.245.168.21/antena2 (para mplayer). <enter>
(now u12 contents are "Antena 2: mms://195.245.168.21/antena2 (para mplayer)."
u12=Arnold Schoenberg: http://81.223.24.100:8000/listen.pls <enter>
(now the contents of u12 are "Arnold Schoenberg: http://81.223.24.100:8000/listen.pls"
So you need to figure out how to treat the newline character in a different way. Also if you can get the work done with a file you can use a file instead of a variable. eg
Did you create the radios file on a windows machine ?
If so you need to edit it and remove the breaks and re-enter them.
I created my radios file using vi.
Why does not the whole output of 'cat radios' go into u12? I suspect it has something to do with the newline char, but do not exactly understand why it fails. Thanks.
Can you check the value of IFS. A newline would be translated to space when you do something like this but should still contain all the lines of the file.
also try with bash -x to see what is happening along with trying to see if the values were stored in array by printing the size of u12 as
Yes. Just after posting I suspected the file from where radio came from could have <CR><LF> line terminators. I used vim's 'set ff=unix' command to remove the carriage return chars, and hexdump showed the <CR>s where gone.
After that, I get the same output as you for the command 'echo $u12' and, yes, I would like the <CR> to remain. Thanks a lot.
EDIT
*** I overlooked the rest of smoker's post. Sorry.
+1 to smoker's idea that the problem in in the file, not in the bash commands and that you need to double quote the variable when echoing it. I don't believe $IFS is relevant except when echoing $u12 without quotes. Here's experimenting with IFS:
Code:
c@CW8:/tmp$ IFS=e
c@CW8:/tmp$ u12=$(cat radios)
c@CW8:/tmp$ echo "$u12"
Antena 2: mms://195.245.168.21/antena2 (para mplayer).
Arnold Schoenberg: http://81.223.24.100:8000/listen.pls
c@CW8:/tmp$ echo $u12
Ant na 2: mms://195.245.168.21/ant na2 (para mplay r).
Arnold Scho nb rg: http://81.223.24.100:8000/list n.pls
c@CW8:/tmp$ unset IFS
c@CW8:/tmp$ echo $u12
Antena 2: mms://195.245.168.21/antena2 (para mplayer). Arnold Schoenberg: http://81.223.24.100:8000/listen.pl
Note: in bash, unsetting IFS is equivalent to it having its default value.
Otherwise, the items will be split into an array using whitespace as the separator.
[PEDANTRY]More precisely: using a space as the separator[/PEDANTRY]
EDIT: even more precisely (!) the value will be split into words everywhere it contains one or more characters that are in $IFS and reassembled with the words separated by a space:
Code:
c@CW8:/tmp$ IFS='
un'
c@CW8:/tmp$ x='Linux Questions
> Rules OK'
c@CW8:/tmp$ echo $x
Li x Q estio s R les OK
c@CW8:/tmp$ echo "$x"
Linux Questions
Rules OK
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.