tags:

views:

200

answers:

3

Hello,

I have a problem putting the content of pwd command into a shell variable that i'll use later. Here is my shell code (the loop doesn't stop):

#!/bin/bash
pwd= `pwd`
until [ $pwd = "/" ]
    do
        echo $pwd
        ls && cd .. && ls 
        $pwd= `pwd` 
    done

Could you spot my mistake please? Thanks a lot for your help ^^

+3  A: 

Try:

pwd=`pwd`

or

pwd=$(pwd)

Notice no spaces after the equals sign.

Also as Mr. Weiss points out; you don't assign to $pwd, you assign to pwd.

John Weldon
great, thank you so much :)will definatly remember that no spaces and $ sign
Samantha
+3  A: 

In shell you assign to a variable without the dollar-sign:

TEST=`pwd`
echo $TEST

that's better (and can be nested) but is not as portable as the backtics:

TEST=$(pwd)
echo $TEST

Always remember: the dollar-sign is only used when reading a variable.

Johannes Weiß
Thank you so much Johannes :)
Samantha
A: 

You can also do way more complex commands, just to round out the examples above. So, say I want to get the number of processes running on the system and store it in the *${NUM_PROCS}* variable.

All you have to so is generate the command pipeline and stuff it's output (the process count) into the variable.

It looks something like this:

NUM_PROCS=$(ps -e | sed 1d | wc -l)

I hope that helps add some handy information to this discussion.

Jim