How does PWD change when you navigate directories?

QuestionsQuestions8 SkillsProDec, 12 2025
0118

That's an excellent follow-up question! It directly gets to the core of how PWD functions dynamically.

The PWD environment variable automatically updates its value every time you change your current working directory using a command like cd.

Let's illustrate this with an example you can try in your terminal:

  1. Start in your home directory:

    echo $PWD
    # You'll likely see something like: /home/labex

    Here, PWD is /home/labex.

  2. Navigate to another directory, for example, your Desktop:

    cd Desktop
    echo $PWD
    # Now you'll see: /home/labex/Desktop

    Notice how PWD immediately reflects the new directory you've moved into.

  3. Go back up one level:

    cd ..
    echo $PWD
    # You'll see: /home/labex

    Again, PWD updates to show your current location.

So, in short, PWD is a dynamic variable that always holds the absolute path of the directory you are currently "standing" in within your terminal session. It's incredibly useful for scripts and applications that need to know their current location.

0 Comments

no data
Be the first to share your comment!