I do have a Bash list (space separated string) and I just want to extract the first string from it.
Example:
VAR="aaa bbb ccc" -> I need "aaa"
VAR="xxx" -> I need "xxx"
Is there another trick than using a for with break?
I do have a Bash list (space separated string) and I just want to extract the first string from it.
Example:
VAR="aaa bbb ccc" -> I need "aaa"
VAR="xxx" -> I need "xxx"
Is there another trick than using a for with break?
Use cut:
echo $VAR | cut --delimiter " " --fields 1 # Number after fields is the
# index of pattern you are retrieving
Try this format:
echo "${VAR%% *}"
Another way is:
read FIRST __ <<< "$VAR"
echo "$FIRST"
If you want arrays, use arrays. ;)
VAR=(aaa bbb ccc)
echo ${VAR[0]} # -> aaa
echo ${VAR[1]} # -> bbb
I'm not sure how standard this is, but this works in Bash 4.1.11
NewVAR=($VAR)
echo $NewVAR
At this moment the only solution that worked, on both Linux and OS X was:
IP="1 2 3"
for IP in $IP:
do
break
done