I have a Bash script with the following lines of code:

echo "current directory is" $PWD

echo $PWD

# put current directory into a variable for use later in script

echo "contents of original directory variable:" originalDirectory

And the resultant output is this: enter image description here

The first 2 lines of output make sense but the 3rd one does not. I expected it to output
contents of original directory variable: /media/sf_code/scripts
but it is just printing the name of the variable. Why doesn't it display the contents of the originalDirectory variable after the colon in that string?


The last line should be

echo "contents of original directory variable:" $originalDirectory

Without the $ it is text, not a variable content. So you get the text.

So it is an expected output ;-)

  • 1
    For echo the variable name wouldn't need double quoutes, but for other context it should, in case there are spaces in the directory. – schrodigerscatcuriosity Dec 14 '19 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.