0

here is my simple command.

ls -lrth ../ | grep file | awk -F" " -v orig=`cd .. | pwd ` -v sort=`pwd` '{print $NF "," $7"/"$8"/"$9","orig"," sort }'

I'm trying to get the value of my previous path just above my current working directory.

current working directory = /home/PC1/Environment/Test1

what i want to get the value of pwd is /home/PC1/Environment and not want to hardcode it.

i tried to use cd .. | pwd but it still displays my current working directory not my previous working directory

can anyone help? some suggestions would be nice.

1 Answers1

1

Use $(cd .. && pwd). You can also use $(cd - && pwd) to get your previous working directory even if it wasn't the parent of your current one. (In general, you should use $(...) instead of `...` to get command output; the latter interferes with quoting and doesn't nest, so can cause surprising results).

Your cd | pwd runs the cd and the pwd at the same time in different subshells, which is not what you want.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175