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.
pwd can be used to find the current working directory, and dirname to find the directory of a particular file (command that was run, is $0 , so dirname $0 should give you the directory of the current script).
The current working directory is the directory in which the user is currently working in. Each time you interact with your command prompt, you are working within a directory. By default, when you log into your Linux system, your current working directory is set to your home directory.
To change to the current working directory's parent directory, type cd followed by a space and two periods and then press [Enter]. To change to a directory specified by a path name, type cd followed by a space and the path name (e.g., cd /usr/local/lib) and then press [Enter].
#!/bin/bash cd "$(dirname "$0")"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With