Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6844437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:22:42+00:00 2026-05-27T00:22:42+00:00

I seem to have failed at something pretty simple, in bash. I have a

  • 0

I seem to have failed at something pretty simple, in bash. I have a string variable that holds the full path to a directory. I’d like to assign the last two directories in it to another string. For example, if I have:

DIRNAME = /a/b/c/d/e

I’d like:

DIRNAME2 = d/e

I’m sure there’s a simple bash construct or sed command that will do it, but it’s escaping me. I’d sort of like a generalized version of basename or dirname that doesn’t just return the extreme parts of a name.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:22:43+00:00Added an answer on May 27, 2026 at 12:22 am
    DIRNAME="/a/b/c/d/e"
    D2=$(dirname "$DIRNAME")
    DIRNAME2=$(basename "$D2")/$(basename "$DIRNAME")
    

    Or, in one line (but be careful with all the double quotes — it is easier when it is split up):

    DIRNAME2=$(basename "$(dirname "$DIRNAME")")/$(basename "$DIRNAME")
    

    Don’t try that game with back-quotes unless you’re heavily into masochism. And if there might be spaces in the paths, use double quotes around the variable names.

    This will work with almost any shell Korn Shell as well as Bash. In bash, there are other mechanisms available – other answers illustrate some of the many options, though expr is also an old-school solution (it was present in 7th Edition Unix too). This code using back-quotes works in Bash and Korn shell too — but not in Heirloom Shell (which is similar to a Unix System V Release 2/3/4 shell, IIRC).

    DIRNAME2=`basename "\`dirname \\"$DIRNAME\\"\`"`/`basename "$DIRNAME"`
    

    (Two levels of nesting is not too awful, but it is pretty bad; three gets really tricky!)

    Testing

    When testing path name manipulation that should survive spaces in the path name, it is worth testing with a name containing double-spaces (rather than, or as well as, single spaces). For example:

    DIRNAME="/a b/ c d /  ee  ff  /  gg  hh  "
    echo "DIRNAME=[[$DIRNAME]]"
    echo "basename1=[[$(basename "$DIRNAME")]]"
    echo "basename2=[[`basename \"$DIRNAME\"`]]"
    echo
    D2=$(dirname "$DIRNAME")
    echo "D2=[[$D2]]"
    DIRNAME2=$(basename "$D2")/$(basename "$DIRNAME")
    echo "DIRNAME2=[[$DIRNAME2]]"
    echo
    DIRNAME3=$(basename "$(dirname "$DIRNAME")")/$(basename "$DIRNAME")
    echo "DIRNAME3=[[$DIRNAME3]]"
    DIRNAME4=`basename "\`dirname \\"$DIRNAME\\"\`"`/`basename "$DIRNAME"`
    echo "DIRNAME4=[[$DIRNAME2]]"
    

    The output from that is:

    DIRNAME=[[/a b/ c d /  ee  ff  /  gg  hh  ]]
    basename1=[[  gg  hh  ]]
    basename2=[[  gg  hh  ]]
    
    D2=[[/a b/ c d /  ee  ff  ]]
    DIRNAME2=[[  ee  ff  /  gg  hh  ]]
    
    DIRNAME3=[[  ee  ff  /  gg  hh  ]]
    DIRNAME4=[[  ee  ff  /  gg  hh  ]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that looks something like this <form action=# class=ajax_form method=post id=comment_form
I have a simple question. I have a model that looks like this: public
I seem to have an app on my Dev server that has lots of
I seem to have missed something - in this step through it talks through
Ruby doesn't seem to have a facility for defining a protected/private block like so:
I've seem to have hit a bug or i have overlooked something. I written
I have something of a simple usecase: User has a set of Privileges; when
OK so we have something that is currently using OpenExeConfiguration for reading a config
I have a device that supports 4-color graphics (much like CGA in the old
Say you have something like an ASP.NET ASP:DetailsView to show and edit a single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.