Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"

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

Try this:

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

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

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




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

You must log in to answer this question.

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