3

I am trying to compare my working directory with a correct directory from which the script needs to run. Here is snippet of shell script

CURR_DIR=echo pwd
echo $CURR_DIR
if [ "$CURR_DIR" == "/proj/project_a/scripts_shell" ]; then
    echo "You are running script from correct directory"
fi

In this case I ensured that I am in /proj/project_a/scripts_shell (as my present working directory) but for some reason it is not able to detect this in the if statement. Thus it is not printing the message.

What is wrong with this?

2 Answers 2

5

Try this:

if [ "$PWD" = "/proj/project_a/scripts_shell" ]; then
    echo "You are running the script from the correct directory" 
fi

The problem is that when you do CURR_DIR=echo pwd, the shell calls pwd with the environment variable CURR_DIR set to echo. When you need to capture the output of a command in a variable, just do VAR=$(cmd).

1
  • 1
    This needs double equals, else it will do assignment not evaluation.
    – trash80
    May 22, 2020 at 19:15
0

This is a notation for saving command result into string in BASH

CURR_DIR=$(pwd)

or

CURR_DIR=`pwd`

When you type CURR_DIR=echo pwd that's make CURR_DIR just a "pwd" string. This is how echo function work. For example execute following line in your shell

echo blah blah blah 

this do nothing with blah command.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .