ravi
ravi

Reputation: 1220

How to store current directory path in variable from bash script for windows?

I am building meteor application and installing this application in client's system via bash script in windows.

In this script, i need to get current directory path stored in variable and then i need to write it in settings.json file.

I tried many solutions but later i found out that all those were for batch script.

mkdir upload
cd upload
DB_IMG_PATH=cd // I need to set path here in any variable and later use it below to write in settings.json file
echo %DB_IMG_PATH%
cd ..

echo '{"public":{"imgUploadUrl":"D:/mssqlempowervisi/upload","adminUser":"[email protected]","adminPassword":"admin@123"}}' >> settings.json

Upvotes: 0

Views: 4759

Answers (1)

Camusensei
Camusensei

Reputation: 1563

Assuming you're really in bash:

mkdir upload
cd upload
DB_IMG_PATH=$PWD
echo "$DB_IMG_PATH"
cd ..

echo '{"public":{"imgUploadUrl":"'"$DB_IMAGE_PATH"'","adminUser":"[email protected]","adminPassword":"admin@123"}}' >> settings.json

And the same thing without unnecessary cd steps:

mkdir upload
DB_IMG_PATH=$PWD/upload
echo "$DB_IMG_PATH"

Please be aware though:

  • By convention, we capitalize environment variables (PAGER, EDITOR, ..) and internal shell variables (SHELL, BASH_VERSION, ..). All other variable names should be lower case. Remember that variable names are case-sensitive; this convention avoids accidentally overriding environmental and internal variables.
  • Never change directories in a script unless you check if it failed! cd $foo is bad. cd "$foo" || exit is good.

Upvotes: 2

Related Questions