# How to extract text from an environment variable?



## mrjayviper (May 10, 2014)

I have a my-variable-here environment variable, e.g.

```
my-variable-here=/var/log/test
```
I want to get the text between the equal sign and the 2nd second slash. So in my example above, I want to get the text "/var". *H*ow do I do it?

*T*hanks very much*.*


----------



## storvi_net (May 10, 2014)

Very quick and dirty without sed (which would be more elegant):


```
$ export my="/var/log/test"
$ echo $my
/var/log/test
$ echo "/$(echo $my | cut -d "/" -f2)"
/var
```


----------



## wblock@ (May 10, 2014)

There's also sh(1) "Parameter Expansion".  See `man sh | less -p 'Remove Largest Suffix Pattern'`, although right now I can't think of an appropriate pattern to leave just the first directory name for an arbitrary-length path.  It could also be done with multiple calls to basename(1)/dirname(1).


----------

