When writing a bash script, how do I get the absolute path of the location of the current file?

dimpol

Suppose I have a bash file called myBash.bash. It resides in:

/myDirect/myFolder/myBash.bash

Now I want to use the string /myDirect/myFolder (the location of myBash.bash) inside the script. Is there a command I can use to find this location?

Edit: The idea is that I want to set-up a zip-folder with code that can be started by a bash script inside that zip-file. I know the relative file-paths of the code inside that zip-file, but not the absolute paths, and I need those. One way would be to hard-code in the path, or require the path of the file to be given as a variable. However I would find it easier if it was possible for the bash-file to figure out where it is on its own and then create the relevant paths to the other file from its knowledge of the structure of the zip-file.

Zanna

You can get the full path like:

realpath "$0"

And as pointed out by Serg you can use dirname to strip the filename like this

dirname "$(realpath $0)"

or even better to prevent awkward quoting and word-splitting with difficult filenames:

temp=$( realpath "$0"  ) && dirname "$temp"

Much better than my earlier idea which was to parse it (I knew there would be a better way!)

realpath "$0" | sed 's|\(.*\)/.*|\1|'

Notes

  • realpath returns the actual path of a file
  • $0 is this file (the script)
  • s|old|new| replace old with new
  • \(.*\)/ save any characters before / for later
  • \1 the saved part

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

When writing a Bash script, how do I create then open a file with the date as part of the name?

분류에서Dev

How do I silence this specific message written to terminal/file (when calling an 'Rscript' via a bash script)

분류에서Dev

How can I get the absolute path of the preference file for a particular plugin in eclipse plugin development?

분류에서Dev

Remove absolute path from file with bash

분류에서Dev

How do I add a location to my path in Unix?

분류에서Dev

Given a path, How do i actually get a file and then readAsDataUrl using Cordova File API?

분류에서Dev

Python / How to get logger with absolute path?

분류에서Dev

How do I update UI when location changes occur?

분류에서Dev

How do I change the location of the apt-get archive?

분류에서Dev

How do I preserve the current viewport when removing a div?

분류에서Dev

how do I center a map on my current location html5

분류에서Dev

How do I create a simple loop daemon bash script?

분류에서Dev

how can I get a some values in strings in bash script

분류에서Dev

How can I set PATH in the `dash`(or bash) shell so that it doesn't search any dirs ? because empty PATH searches current directory

분류에서Dev

Writing a bash script to read a text file of numbers into arrays, by column, on Ubuntu

분류에서Dev

How can I write a bash script to search all files in current directory for multiple strings?

분류에서Dev

How to get Absolute path from android directory URI

분류에서Dev

How do I know the location of the Python executable from within the Python script?

분류에서Dev

How do I pass a file as an argument in bash scripting?

분류에서Dev

How do I prevent a script from terminating when the shell exits?

분류에서Dev

How can I make a bash counter script work when executing it from another script?

분류에서Dev

How to write a script to open current path from nautilus in guake

분류에서Dev

How do I ensure that the right ViewController is loaded when accepting a file?

분류에서Dev

how do I get my partition to mount in the file manager?

분류에서Dev

How do I click button and get excel file by C#?

분류에서Dev

How do I use OptiPNG on Windows to get a lossless file?

분류에서Dev

How do I get the name of Postscript file object?

분류에서Dev

How do I get a CR/LF when setting textContent?

분류에서Dev

How do i get this kind of time format when "now" in javascript?

Related 관련 기사

  1. 1

    When writing a Bash script, how do I create then open a file with the date as part of the name?

  2. 2

    How do I silence this specific message written to terminal/file (when calling an 'Rscript' via a bash script)

  3. 3

    How can I get the absolute path of the preference file for a particular plugin in eclipse plugin development?

  4. 4

    Remove absolute path from file with bash

  5. 5

    How do I add a location to my path in Unix?

  6. 6

    Given a path, How do i actually get a file and then readAsDataUrl using Cordova File API?

  7. 7

    Python / How to get logger with absolute path?

  8. 8

    How do I update UI when location changes occur?

  9. 9

    How do I change the location of the apt-get archive?

  10. 10

    How do I preserve the current viewport when removing a div?

  11. 11

    how do I center a map on my current location html5

  12. 12

    How do I create a simple loop daemon bash script?

  13. 13

    how can I get a some values in strings in bash script

  14. 14

    How can I set PATH in the `dash`(or bash) shell so that it doesn't search any dirs ? because empty PATH searches current directory

  15. 15

    Writing a bash script to read a text file of numbers into arrays, by column, on Ubuntu

  16. 16

    How can I write a bash script to search all files in current directory for multiple strings?

  17. 17

    How to get Absolute path from android directory URI

  18. 18

    How do I know the location of the Python executable from within the Python script?

  19. 19

    How do I pass a file as an argument in bash scripting?

  20. 20

    How do I prevent a script from terminating when the shell exits?

  21. 21

    How can I make a bash counter script work when executing it from another script?

  22. 22

    How to write a script to open current path from nautilus in guake

  23. 23

    How do I ensure that the right ViewController is loaded when accepting a file?

  24. 24

    how do I get my partition to mount in the file manager?

  25. 25

    How do I click button and get excel file by C#?

  26. 26

    How do I use OptiPNG on Windows to get a lossless file?

  27. 27

    How do I get the name of Postscript file object?

  28. 28

    How do I get a CR/LF when setting textContent?

  29. 29

    How do i get this kind of time format when "now" in javascript?

뜨겁다태그

보관