How to get the current path in shell and assign it to a variable?

You can use the pwd command to get the current path, and then store this path in a variable. Here is an example:

current_path=$(pwd)
echo "当前路径是: $current_path"

In this example, the pwd command is used to fetch the current path, which is then assigned to the variable current_path. The echo command is then used to print out the current path.

Furthermore, you can also directly use $PWD to obtain the current path, as shown below:

current_path=$PWD
echo "当前路径是: $current_path"
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds