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.
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|'
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] 삭제
몇 마디 만하겠습니다