3 Replies Latest reply on Feb 2, 2014 7:12 PM by Dude!

    export `dirname $0` does not return value




      I have a sheel script  that works well.

      Recent days, it does not work. Per debug, export PLOC=`dirname $0` does not return a value.

      I run script as bush shell in  Oracle Linux Server release 5.10 Red Hat Enterprise Linux Server release 5.10 (Tikanga)


      I try to PLOC=`dirname -- $0` or PLOC=`dirname BASH_ARGV[0]` or PLOC=`dirname {BASH_ARGV[0]}` But all of changes does not work.


      How do we get a value from dirname?


      Thanks so much


        • 1. Re: export `dirname $0` does not return value

          Sorry, but there is no information to be able to troubleshoot your problem.


          $0 is the name of the running process. When used inside a shell, it will return the name of the shell. When used inside a script, it will be the name of the script. When source executing a script, it  will return the name of the shell. Your shell can be a login shell (-bash) or subshell (bash).


          The commands dirname and basename will fail if $0 contains the the name of the login script. For instance:


          dirname -bash

          dirname: invalid option -- 'b'

          Try `dirname --help' for more information.


          I think this question is already answered here: dirname and basename did not work after linux patch


          I suggest to close one of the two threads, otherwise a forum moderator might consider it a duplicate post, which is not welcome, and lock your thread.


          In order to troubleshoot your script, simply add "echo $0" before using $0 to see what it returns.

          • 2. Re: export `dirname $0` does not return value

            Thanks your advise.

            echo $0" return shell name test.sh but nothing is for  PLOC=`dirname -- $0`


            What kind of issue is for this

            • 3. Re: export `dirname $0` does not return value

              A simple test often helps to troubleshoot or understand a particular function. I suggest to execute the following test script and post your output if there is any difference.


              [oracle@vm110 ~]$ uname -r



              [oracle@vm110 ~]$ cat test.sh

              echo $0

              PLOC=`dirname $0`

              echo $PLOC


              [oracle@vm110 ~]$ /home/oracle/test.sh



              [oracle@vm110 ~]$ ./test.sh



              [oracle@vm110 ~]$ . ./test.sh


              dirname: invalid option -- b

              Try `dirname --help' for more information.