How to get the Current Working Directory in Bash

Bash, or the Bourne Again SHell, is a powerful command line interpreter widely used in Unix and Linux systems. One of the fundamental concepts in shell scripting is the notion of the current working directory. This article is designed to provide a comprehensive understanding of this concept, suitable for beginners, and also includes examples for more advanced users.

In this tutorial you will learn:

  • Current Working Directory Basics: Learn the key concept of the current working directory in Unix-like systems.
  • `pwd` Command Usage: Understand how to use `pwd` to display the current directory in Bash.
  • Advanced Scripting Techniques: Explore advanced uses of `pwd`, including variable storage and file operations.
  • Example Scripts: Delve into practical script examples, from basic to complex directory operations.
How to get the Current Working Directory in Bash
How to get the Current Working Directory in Bash
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

What is the Current Working Directory?

In Unix-like operating systems, every process, including a Bash shell or script, operates in a specific directory called the ‘current working directory’. This is the directory in which the process starts and is typically the directory from where you run your script or command.

Understanding the current working directory is crucial for various tasks, such as reading, writing, and executing files, or managing paths.

Basic Usage: pwd Command

The simplest way to find the current working directory in Bash is by using the pwd command, which stands for “print working directory.”

  1. #!/bin/bash
    echo "Current Directory:"
    pwd

    When this script is executed, it prints the absolute path of the current working directory.

  2. Bash Script with Relative Path Operations
    #!/bin/bash
    echo "Starting in directory:"
    pwd
    
    cd ../
    echo "Now in directory:"
    pwd




    This script shows how the current directory changes after a cd (change directory) command.
  3. For more advanced scenarios, you might need to use the output of pwd in combination with other commands or in scripts.
    #!/bin/bash
    current_dir=$(pwd)
    echo "The script is running from: $current_dir"

    Here, $(pwd) executes the pwd command and its output is stored in the variable current_dir.

  4. For more advanced scenarios, you might need to use the output of pwd in combination with other commands or in scripts.
    #!/bin/bash
    log_file="$(pwd)/log.txt"
    echo "Log file created in current directory."
    touch "$log_file".

    This script creates a log file in the current working directory, demonstrating how pwd can be used in file path specifications.

  5. Let’s consider a more complex example where you need to check if a list of directories exists and then move to the first existing directory.
    #!/bin/bash
    directories=("/path/to/dir1" "/tmp" "/path/to/dir2")
    
    for dir in "${directories[@]}"; do
      if [ -d "$dir" ]; then
        echo "Moving to $dir"
        cd "$dir"
        break
      else
        echo "$dir does not exist."
      fi
    done
    
    echo "Current directory:"
    pwd

    This script iterates over a list of directories and changes the current working directory to the first one that exists.

    Bash script output of finding existing directory and changing to the first directory it can find from the list
    Bash script output of finding existing directory and changing to the first directory it can find from the list

Conclusion

Understanding and manipulating the current working directory is a fundamental skill in Bash scripting. Starting with simple pwd usage, you can progressively handle more complex scenarios involving directory navigation and file operations. Remember, consistent practice and experimenting with different scenarios are key to mastering Bash scripting.

FAQ:

1. What does pwd stand for?

‘pwd’ stands for ‘Print Working Directory.’

2. Is pwd specific to Bash?

No, pwd is available in most Unix-like shells.

3. Can pwd output be customized?

No, pwd typically outputs the absolute path.

4. Does pwd change the directory?

No, it only displays the current directory.

5. How does pwd differ from $PWD?

pwd is a command, while $PWD is an environment variable.

6. Can pwd work with symbolic links?

Yes, but it shows the resolved path, not the symlink.

7. How to store pwd output in a file?

Use pwd > filename.txt to redirect the output to a file.

8. Can pwd be used in a cron job?

Yes, pwd can be used in cron jobs for path-related tasks.

9. What happens if pwd is run in a deleted directory?

It will still display the path of the deleted directory.

10. How to use pwd in a Bash function?

Use pwd within a function to return or process the current directory.

11. Does pwd work in subshells?

Yes, it displays the current directory of the subshell.

12. Can pwd handle hidden directories?

Yes, pwd shows the path of hidden directories as well.

13. Is pwd affected by directory permissions?

Only if permissions prevent accessing the directory.

14. Can pwd be aliased to a custom command?

Yes, you can alias pwd to include additional functionality.

15. Are there alternatives to pwd in Unix systems?

While pwd is standard, some scripts use $PWD for similar purposes.



Comments and Discussions
Linux Forum