I have a variable containing a list of strings, and want to create a string with the first character being -.
Example:
var="-name fred paul -surname winnett dimech"
I want a string, namely
errStr="-name -surname"
I have a variable containing a list of strings, and want to create a string with the first character being -.
Example:
var="-name fred paul -surname winnett dimech"
I want a string, namely
errStr="-name -surname"
try this
errStr=`echo $var | tr -s " " "\n" | grep - | tr "\n" " "`
There's no need for any external command:
var="-name fred paul -surname winnett dimech"
errStr=
for word in $var
do
case $word in
-*) errStr=${errStr:+"$errStr "}$word ;;
esac
done