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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:45:21+00:00 2026-06-16T00:45:21+00:00

The only difference in Sourcing a Script and Executing it, I was aware of,

  • 0

The only difference in Sourcing a Script and Executing it, I was aware of, is in the later case, the parent environment remains unaffected.

Consider this sample Script:

SETUP_DIR=`dirname $0`
echo $SETUP_DIR
echo $0
echo $1

The above script simply fetches the directory name of the running script. While trying to source it, the above script fails with the following error

$ . test.sh foo
dirname: invalid option -- k
Try `dirname --help' for more information.

-ksh
foo

The reason what it seems to me is, when sourcing it, as the script runs in the parent environment shell, the shell name is passed as the first argument, in this case its -ksh.

and dirname -ksh fails because of an -k, which is an invalid option for dirname

Strangely, this problem is not there while running in sh or bash

$ sh
sh-3.2$ . test.sh foo
.
sh
foo
sh-3.2$ bash
bash-3.2$ . test.sh foo
.
bash
foo
bash-3.2$
  • So I was wondering, is it a known documented behavior in KornShell (ksh)? How to resolve this issue?
  • My second generic question is, why does dirname 'sh' returns the current directory .
  • 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-06-16T00:45:21+00:00Added an answer on June 16, 2026 at 12:45 am

    So I was wondering, is it a known documented behavior in corn shell? How to resolve this issue?

    When ksh is started as a login shell by /bin/login, its $0 will be prefixed with -, so $0 will be -ksh instead of just ksh or /bin/ksh. Login does this to indicate to the shell that it is being started as a login shell, as this typically affects profile loading.

    The ksh manual documents its adherence to this convention:

    If the shell is invoked by exec(2), and the first character of argument zero ($0) is -, then the shell is assumed to be a login shell and commands are read from […]

    The hyphen prefix doesn’t show up for me with bash, I assume that bash is editing the command line to stop it from being visible in the process listing.

    As a result, dirname $0 is dirname -ksh, and dirname tries to parse that as passing the k, s and h options

    You can get around this by explicitly terminating options parsing by dirname with --, i.e.

    dirname -- "$0"
    

    My second generic question is, why does dirname ‘sh’ returns the current directory .

    dirname is trying to be helpful and return a usable path even when there is no directory part of the path you’re giving it. Since your “$0” only has one part, it assumes it is relative to the current working directory, i.e. sh == ./sh, and returns the .

    This is documented as per dogbane’s answer

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

Sidebar

Related Questions

Is there anyway this code can be refactored? The only difference is the order
Is there a way to DRY this CSS up? Only difference is color? div.base-text-gold
Consider two following pieces of code - the only difference between them is a
1.parent > son 2.parent>son the only difference is whether there is space before and
I have almost the same problem as this unanswered question . The only difference
I've got a similar to this post problem, the only difference is that it's
I'm reading a C++ tutorial and I've ran into this sentence: The only difference
Similar to this question: Apache/Rails/Passenger Displaying Site Index? Only difference is that my app
I have two tables and their only difference is one column is missing from
I'm trying to clean duplicate code. The only difference are calls like MyType x

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.