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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:45:06+00:00 2026-05-16T01:45:06+00:00

I’m writing a Bash script. I need the current working directory to always be

  • 0

I’m writing a Bash script. I need the current working directory to always be the directory that the script is located in.

The default behavior is that the current working directory in the script is that of the shell from which I run it, but I do not want this behavior.

  • 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-16T01:45:07+00:00Added an answer on May 16, 2026 at 1:45 am

    TL;DR

    #!/bin/bash
    cd "$(dirname "$0")"
    

    The long winded explanation

    How does this work and how does it deal with edge and corner cases?

    You type a command launching the script in your interactive shell. That interactive shell calls bash with some name which tells bash the script to run. That bash then calls dirname with the bash argument which points to the script. Finally, that bash then calls cd with the output of dirname as its argument.

    script invocation command bash argument dirname argument cd argument
    foo (found in $PATH at /path/to/foo) /path/to/foo /path/to/foo /path/to
    bash foo foo foo .
    /foo /foo /foo /
    ./foo ./foo ./foo .
    "/pa th/to/foo" /pa th/to/foo /pa th/to/foo /pa th/to
    "./pa th/to/foo" ./pa th/to/foo ./pa th/to/foo ./pa th/to
    "../pa th/to/foo" ../pa th/to/foo ../pa th/to/foo ../pa th/to
    "../../pa th/to/foo" ../../pa th/to/foo ../../pa th/to/foo ../../pa th/to
    "pa th/to/foo" pa th/to/foo pa th/to/foo pa th/to
    --help/foo --help/foo * N/A N/A
    --help N/A ** N/A N/A

    On symlinks

    The cd command will follow symlinks if they are involved. A symlink usually exists to be followed, so in most situations following the symlink is the correct thing to do. Why would it be a problem for the code in the script to follow a symlink when it was just fine to follow that same symlink a few microseconds ago when loading the script?

    On command arguments starting with hyphens

    * There is only one case where the argument to the dirname could begin with a -, and that is the relative path case --help/foo. If the script is in a subdirectory called --help, the script execution will run bash --help/foo, and bash does not know that option --help/foo and will therefore abort with an error message. The script will never execute, so it does not matter what the cd "$(dirname "$0")" would have executed.

    ** Note that calling the script --help makes the shell not find the command when you are typing --help in the same directory. Alternatively, with $PATH containing the current directory, the script will be run as /path/to/--help or ./--help, always with something in front of the - character.

    Unless bash introduces command line arguments with a parameter separated by a =, it is unlikely that you could pass a - argument to bash which contains a / later, and which is accepted by bash.

    If you can rely on dirname accepting -- argument (bash builtin cd will certainly accept --), you can change the script snippet to

    cd -- "$(dirname -- "$0")"
    

    Please do comment if you can figure out a way to construct an argument beginning with - which can be sneaked past bash.

    Nota bene

    The above snippet also works with non-bash /bin/sh.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

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.