Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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).

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.