How to use a shell script to get the current directory?
To fetch the current directory using a shell script, you can use the built-in environment variable PWD or the $(pwd) command to obtain the current working directory. Here are two methods to do so:
- People with disabilities
current_dir=$PWD
echo "当前目录是:$current_dir"
- Display the current working directory.
current_dir=$(pwd)
echo "当前目录是:$current_dir"
Both methods will obtain the current working directory of the shell and assign it to the variable “current_dir”, then print it out.