Where to find the source of a shell script?

Where to find the source of a shell script?

The location of the sourced script is not available unless you are using a shell that offers extensions to the POSIX specification. You can test this with the following snippet: In bash, the name of the sourced script is in $BASH_SOURCE.

How to change the source of a bash script?

You can also change the source with a dot command which does the same thing but is supported in both bourne and bash. Now the script will work with either sh or bash. You should also run the script directly to avoid confusions like these by making it executable chmod +x xRUN, and invoking it like this:

How to determine the path of a script?

The following code snippet can be used in the script being sourced to determine its absolute path. Tested in bash, zsh and dash invoked both as dash and sh. NB: lsof (1) options should be verified as well because similar advices both from this and other pages did not work for me on Ubuntu 18 and 19 thus I had to reinvent this.

How do I know the source file name in Bash?

The reason the one-liner works is explained by the use of the BASH_SOURCE environment variable and its associate FUNCNAME. An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined.

How to find out which Directory the script file resides?

– Stack Overflow Unix shell script find out which directory the script file resides? Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides?

How can I get the source directory of a bash script?

Basically the code would have to redo what the OS does. One way to do what you are asking would be to just hardcode the data in the /usr/share directory, and reference it by its full path. Data shoudn’t be in the /usr/bin directory anyway, so this is probably the thing to do.

How to change the Directory of a shell script?

Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides? The original post contains the solution (ignore the responses, they don’t add anything useful).

How to find the source path of a script?

ScriptDirFinder.ps1 Import-Module ScriptDirFinder -force “top level:” Split-Path $script:MyInvocation.MyCommand.Path #$foo = (Get-Variable MyInvocation -Scope 1).Value #Split-Path $foo.MyCommand.Path “immediate func call” Get-ScriptDirectory1 “dot-source call” Get-ScriptDirectory2 “module call” Get-ScriptDirectory3 NestCall DoubleNested

How to determine the path to a shell script?

The element with index 0 is the name of any currently-executing shell function. The bottom-most element (the one with the highest index) is “main”. This variable exists only when a shell function is executing. Assignments to FUNCNAME have no effect and return an error status.

How to determine the path of dash shell?

The trickiest part is finding currently sourced file is for dash shell used as sh replacement in Ubuntu. The following code snippet can be used in the script being sourced to determine its absolute path.