Linux / Unix Shell Script: Get The Current Directory

You can get the current directory in a shell script using the pwd command. However, it’s recommended to use the $PWD shell variable, which is automatically set to the current working directory.

Here’s an example shell script that prints the current directory:

#!/bin/bash

echo "Current directory: $PWD"

You can also use the $(pwd) command substitution to get the current directory:

#!/bin/bash

echo "Current directory: $(pwd)"

Leave a Comment