9

Why am I getting this error, I am trying to source this set.conf

user@connect:~/testenv$ source $(cd $(dirname $0) && pwd)"/set.conf"
dirname: invalid option -- 'b'
Try 'dirname --help' for more information.
-bash: /home/tempuser/set.conf: No such file or directory

I tried adding -- $0 on command prompt it works well but. How can I run this in script?

2
17

$0 inside a script is the script's name.

At the prompt, it's the shell's name, which is probably -bash in your case.

dirname is treating the leading dash as introducing a command line option, -b, which it doesn't support.

  1. The code you're using won't work at the prompt. You could paste it inside a file and run the file instead.
  2. Try dirname -- $0 instead: most commands let you write -- to make it treat everything that follows as parameters instead of options. In this case, it won't fix the problem, but it should make it clearer what the problem is.
3
  • it got executed, but as you said it is working in bash prompt but it is not getting executed in the actual script file. – AReddy Feb 2 '17 at 7:35
  • How can I execute it on the script.... – AReddy Feb 2 '17 at 7:44
  • Nice & intuitive answer. – holi-java Jan 14 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.