How to set current working directory to the directory of the script?
问题 I'm writing a bash script. I need the current working directory to always be the directory that the script is located in. The default behavior is that the current working directory in the script is that of the shell from which I run it, but I do not want this behavior. 回答1: #!/bin/bash cd "$(dirname "$0")" 回答2: The following also works: cd "${0%/*}" The syntax is thoroughly described in this StackOverflow answer. 回答3: Try the following simple one-liners: For all UNIX/OSX/Linux dir=$(cd -P --