On Sun, Sep 18, 2011 at 09:20:21PM +0200, Jan Kanis wrote: > On Sat, Sep 17, 2011 at 07:08, Robert Shinka <[3]k...@unknownlayer.net> > wrote: > > Notice how fish handily split the resulting string at newlines into > separate array elements, and then expanded the result using spaces. > For the typical case of processing data from line-oriented utilities > like sed, this is exactly what you do /not/ want, as the simple act of > storing data into a variable and retrieving it changes the data. > > For the common case, I think this is what you want. In unix programs having > output in a form of one line per item is very common. For example filenames > containing spaces are handled correctly, unlike in bash. Try > > set files (ls) > for i in $files > echo $i > end
The problem is not the splitting, but that the common array expansion uses a different token to expand than it does to split. This doesn't occur in the above example because... well, see below. > You may be wondering what the point of the last two echo commands are. > For the first, fish expands $var to "'1 2' '3' '' '4'", which echo then > happily prints, while for the second, fish formats to "1 2 3 4" first. > The first form can usually be handled with xargs (although this ideally > wouldn't be necessary), but the second is broken any way I look at it. > > However, because there's no sane way to pipe the data to xargs (you > weren't thinking of using echo, were you?) after retrieving it from a > variable, you're reduced to the annoying hack you thought up earlier. > Well, unless you decided to forego variables and instead dump everything > into temporary files and use xargs -a ... > > My specific problem isn't with variable/array handling, just with command > substitution. I don't see exactly what you think is wrong with "$var", if you > need $var or "$var", you can pick the right one. For command substitution, > this choice doesn't exist. In your example, you use $var, which expands to a list of tokens and does "the right thing" in most circumstances. However, what does one use "$var" for? You can't use it to reconstruct the original string, because that string was split on newlines and "$var" replaces them with spaces. It doesn't seem to me that the current form of "$var" is "the right one" very often, if at all. > Bash treats command substitution and variable expansion as a list of > whitespace separated tokens. It also includes escapes to treat both as a > single token, in case you don't want the default behaviour. > > Fish does the same for variables, except it doesn't split the contents when > the variable is used, but it stores the tokens in a structured way as an > array. Command substitution works a bit differently. Maintaining a structured > form of data like with variables isn't possible because it is output from > arbitrary commands, so fish splits the command output like bash does, but > only > on newlines. However fish doesn't provide a way to prevent the splitting. If fish reconstructed "$var" the same way it split it (with \n), then your problem wouldn't exist. You could work with lines as usual with $var, and "$var" would produce the exact data it was created with. If anyone needed a space-separated version of the string for whatever reason, they could trivially do so with echo or tr, but I think that's a rare case. Right now, there's no reasonable utility. ------------------------------------------------------------------------------ BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA http://p.sf.net/sfu/rim-devcon-copy2 _______________________________________________ Fish-users mailing list Fish-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/fish-users