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 6567709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:22:09+00:00 2026-05-25T14:22:09+00:00

How can I put the directory (not full path) of the file which opened

  • 0

How can I put the directory (not full path) of the file which opened the script into a variable? I assume this is the same as $1, so I would like file’s path to yield the same result when executed from terminal.

Explained;

#!/bin/bash
script.sh '~/foo/bar/file.ext'

#I want to put "bar" into a variable.

It should also work when “file.ext” opens with “script.sh” through GUI.

  • 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-25T14:22:10+00:00Added an answer on May 25, 2026 at 2:22 pm

    You should checkout both the dirname and basename programs, The dirname program takes an argument and removes the name of the program from the rest of the path. That is, it will give you the directory name.

    The basename program does the opposite. Given the name of a path, it will remove all directories and just leave the file name:

    $ dirname /one/two/three/four/five.txt
    /one/two/three/four
    $ basename /one/two/three/four/five.txt
    five.txt
    

    Now, the rest depends upon the shell. In BASH (which is the default shell on Linux and what you’ve tagged, you can use the $(command) syntax. This takes the output of the command and replaces it on the command line. For example:

    $ mydirectory=$(dirname /one/two/three/four/five.txt)
    $ echo $mydirectory
    /one/two/three/four
    

    In the above example, the dirname command took the name of the directory, replaced everything in the $() syntax and allowed me to set the name of my variable mydirectory.

    You can use them in combination to get what you want:

    $ my_full_dir=$(dirname "~/foo/bar/file.ext")
    $ echo $my_full_dir
    ~/foo/bar
    $ my sub_dir=$(basename $my_full_dir)
    $ echo $sub_dir
    bar
    

    You can also combine the basename and dirname commands together:

    $ my_sub_dir=$(basename $(dirname "~/foo/bar/file.ext"))
    $ echo $my_sub_dir
    $ bar
    

    When a shell program executes, it puts each and every parameter on the command line into a count variable. For example:

    $ myprog alpha beta gamma delta
    

    Inside of the program myprog, the following variables are set:

    $1 = "alpha"
    $2 = "beta"
    $3 = "gamma"
    $4 = "delta"
    

    One more thing with the BASH shell: There’s a special filter syntax to parse variables.

    • ${variable#pattern} – Left small pattern. Removes the smallest pattern from the left side of the variable
    • ${variable##pattern} – Left large pattern. Removes the largest pattern from the left side of the variable
    • ${variable%pattern} – Right small pattern. Removes the smallest pattern from the right side of the variable
    • ${variable%%pattern} – Right large pattern. Removes the largest possible pattern from the right side of the variable

    Here’s an example:

    $ FOO="ONE|TWO|THREE|FOUR"
    $ echo ${FOO##*|}
    FOUR
    $ echo ${FOO#*|}
    TWO|THREE|FOUR
    

    In the above cases, the pattern was *|. This means any combination of letters followed by a | In the first one, the smallest match was ONE|. In the second one, it was ONE|TWO|THREE|. You can also use this to simulate the basename and dirname commands:

    $ myfile="~/foo/bar/file.ext"
    $ echo ${myfile%/*}     #Like `dirname`
    ~/foo/bar
    $echo ${myfile#*/}      #Like `basename`
    file.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For normal .aspx pages I can just put a Form.browser file into the App_Browsers
With VIM or Emacs I can put comments in the file that will be
I'm trying to understand when I can put certain objects into a users session
With the regular statement (just statement) I can put java enums into the queries
I created simple REST web app that would create Excel file and put into
My app is using iTunes file sharing which exposes everything in the Documents directory
OK, so I know you can put $Revision$ in a file, and then set
how i can put multiple buttons in scroll view with according to my requirement?
In C++ you can put a member in a base class and a member
I know I can put escaped HTML tags in string resources. However, looking at

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.