GhostCat GhostCat - 1 year ago 63
Linux Question

bash: $0 gives different results on Redhat versus Ubuntu?

I have the following script created by some self-claimed bash expert:

SCRIPT_LOCATION="$(readlink -f $0)"

That runs nicely on my local Ubuntu 16.04. Now I wanted to use it on our RH 7.2 servers; and there I got an error message from
; about being called with bad parameters.

Then I figured: on Ubuntu, $0 gives "bash"; whereas on RH, it gives "-bash".

EDIT: script is invoked as


  1. Any idea why that is?

  2. When I change my script to use a hardcoded
    readlink -f bash
    the whole things works. Are there "better" ways for fixing this?

  3. Feel free to also explain what readlink -f bash is actually doing ;-)

123 123
Answer Source

As the script is sourced the readlink -f $0 is pointless as it will just show you the command used to run the shell you are currently using.

To explain the difference in command lets look at the bash man page:

A login shell is one whose first character of argument zero is a -, or one started with the --login option.

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

So guessing ubuntu starts with the noprofile option.

As for readlink, we can again look at the man page

-f, --canonicalize canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist

Therefore it follows symlinks to the base.

Using readlink -f with any non qualified path will result in it just appending the last arg to your current working directory which will not actually show where the script is run.

Try putting any random string instead of bash after it and will see the script is unaffected.


readlink -f dafsfdsf


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download