bash - Assigning echo and cut command to a variable -


i'm trying string "boo" "upper/lower/boo.txt" , assign variable. trying

name= $(echo $whole_thing | rev | cut -d "/" -f 1 | rev | cut -d "." -f 1) 

but comes out empty. doing wrong?

don't way @ all. more efficient use built-in shell operations rather out-of-process tools such cut , rev.

whole_thing=upper/lower/boo.txt name=${whole_thing##*/} name=${name%%.*} 

see bashfaq #100 general introduction best practices string manipulation in bash, or the bash-hackers page on parameter expansion more focused reference on techniques used here.


now, in terms of why original code didn't work:

var=$(command_goes_here) 

...is correct syntax. contrast:

var= $(command_goes_here) 

...exports empty environment variable named var while running command_goes_here, , while running output of command_goes_here own command.


to show yet variant,

var = command_goes_here 

...runs var command, = first argument, , command_goes_here subsequent argument. say, whitespace important. :)


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -