Reputation: 31
I have a variable : email="[email protected]"
and I want to get its username portion to be stored in another variable.
I used the following grep
command:
$ $email | grep '.*@'
Which gave me following output.
yask123@
gmail.com
I want to save the matched string in a variable.
I tried
res=`echo $email | grep '.*@'`
which didn't work.
Upvotes: 2
Views: 2439
Reputation: 69208
Just in case you want to do something more complex than shell match, which is absolutely valid applied to your example:
res=$(echo $email | sed 's/\(.*\)@.*/\1/')
Upvotes: 1
Reputation: 88636
Try this:
email="[email protected]"
echo "${email%@*}"
Output:
yask123
See: 3.5.3 Shell Parameter Expansion
Upvotes: 2