If I have a string such as


or just


how can I split this so that I just have two variables (the path and the application)



I've seen numerous examples of splitting strings, but how can I get just the last part, and combine all the rest?

  • 2
    Did you try dirname and basename?
    – user62916
    Jan 12, 2016 at 16:16

2 Answers 2


The commands basename and dirname can be used for that, for example:

$ basename /home/user/a/directory/myapp.app 
$ dirname /home/user/a/directory/myapp.app 

For more information, do not hesitate to do man basename and man dirname.

  • 1
    You beat me to it :) LOL.
    – Rob
    Jan 12, 2016 at 16:18
  • 27 seconds faster than me. damn :)
    – user34720
    Jan 12, 2016 at 16:18
  • 2
    Sorry for that, I got luck I think. :-)
    – perror
    Jan 12, 2016 at 16:18
  • cheers quys, I figured it would be something simple that I'd overlooked, but not quite that simple!!
    – IGGt
    Jan 12, 2016 at 16:20
  • Somethimes, you just need to forget about strings, and use the manpages to quick look to what you nee ;) man -k "pathname" should be enough to help you on this.
    – user34720
    Jan 12, 2016 at 16:23

With any POSIX shell:

$ str=/home/user/a/directory/myapp.app
$ path=${str%/*}
$ app=${str##*/}
$ printf 'path is: %s\n' "$path"
path is: /home/user/a/directory
$ printf 'app is: %s\n' "$app"
app is: myapp.app

save you for two processes forking.

In case of /myapp.app, myapp.app and /path/to/myapp.app, basename/dirname are more graceful. See also this question for more discussion.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .