Bash script awkwardness with pwd

PWD is an environmental variable and is changed when you change the directory.

Use a different name for the variable,

eg:

MYPWD=${PWD}  #or MYPWD=$(pwd)
cd /etc/nginx/sites-enabled/
cd $MYPWD

Try:

PWD=`pwd`

Or:

PWD=$(pwd)

Both expressions will execute the pwd command and store the command output in the shell variable PWD. There is plenty of discussion on the web about when to use each style. The one point that I recall is that the "$(cmd)" approach allows for nesting of commands, e.g.

CURRENT_BASENAME=$(basename $(pwd))  

Edit - It just occurred to me that PWD is a built in shell variable that always expands to the current working directory.

Tags:

Bash

Cd

Pwd