[nSLUG] Extracting first char from shell var

D G Teed donald.teed at gmail.com
Thu Feb 13 14:51:07 AST 2014


Say I've got a username in a shell variable
mailuser with the value "testuser".  I want to extract
the first letter in the user name because it
is needed to walk the cyrus mail directory tree.

So one answer is:

echo ${mailuser:0:1}
t

Now that we've got that out of the way, I'm curious
what is happening in this:

echo $mailuser | grep -Eo '[a-z]{1}'
t
e
s
t
u
s
e
r

Same without the pipe:

cat /tmp/testfile
testuser

grep -Eo '[a-z]{1}' /tmp/testfile
t
e
s
t
u
s
e
r


Whereas I've used patterns in -Eo to extract IP addresses,
and this works as expected:

grep -Eo ' [a-z]{9}$' /etc/hosts
 localhost
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nslug.ns.ca/pipermail/nslug/attachments/20140213/211937ce/attachment.html>


More information about the nSLUG mailing list