On 2011-05-17, Alex Schuster <[email protected]> wrote:
> Juan Diego Tascón writes:
>
>> I have always wondered if there is a way to do awk '{ print $1}' using
>> only builtin bash functions when you only have a one line string
>
> str="one two five"
>
> # remove all from the first blank on, but will not work with
> # other whitespace
> echo ${str%% *}
>
> or
>
> # set $1, $2, $3, ... to words of $str
> set $str
> echo $1
>
> or
>
> # create array holding one word per element
> strarr=( $str )
> echo $strarr  (or echo ${strarr[0]})
>
>       Wonko
>
>

How about this:

str="one two three"
read word etc <<< "$str"
echo $word

(not tested, though)

--
Pandu E Poluan - IT Optimizer
My website: http://pandu.poluan.info/

Reply via email to