Given a path like this test/90_2a5/Windows
I am trying to get the result 90_2a5 using the commands dirname
to get the path and after basename
to get the name.
The problem occurs when i try to make it in a single line trying to pipe the results from dirname
to basename
.
I have tried this but seems that i am using it the wrong way.
path="test/90_2a5/Windows"
finalName= basename var | dirname $path
echo "$finalName"
The problem is that the finalName is an empty string, meaning that the results from dirname
are not redirecting.
You don't pipe them, but rather pass them as parameters:
finalName=$(basename -- "$(dirname -- "$path")")
Some commands quite simply do not accept input streams, but only parameters:
$ echo foo/bar | basename
basename: missing operand
Try `basename --help' for more information.
$ basename foo/bar
bar
$ echo 'test/90_2a5/Windows' | xargs dirname | xargs basename
90_2a5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With