Mack Wallace <[email protected]> once said:
> My question: Is there a way to take the string output of a command that
> contains spaces and make it a single element of an array in rc script?
>
> [...]
>
> However, if I am receiving output from sed that contains spaces from
> the following script line
>
> string_var = `{echo some_string | sed ’s/x/y/g’}
>
> If the output was ‘hello world’, string_var would become a two
> element array which
> echo $some_string(1)
> echo $some_string(2)
>
> Would output
> hello
> world
You need to change the input field separator, $ifs.
The default value is any whitespace character:
% x = `{echo -n hello world}
% echo $#x
2
% echo $x(1)
hello
% echo $x(2)
world
Using only newline as the separator yields:
% ifs = '
' # this is a newline character
% y = `{echo -n hello world}
% echo $#y
1
% echo $y(1)
hello world
You might want to use a comma instead:
% ifs = ,
% z = `{echo -n hello world,good luck}
% echo $#z
2
% echo $z(1)
hello world
% echo $z(2)
good luck
Cheers,
Anthony