How to assign the output of a Bash command to a variable?

BashShell

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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.

Solution 3 - Bash

In this specific case, note that bash has a variable called PWD that contains the current directory: $PWD is equivalent to `pwd`. (So do other shells, this is a standard feature.) So you can write your script like this:

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

Note the use of double quotes around the variable references. They are necessary if the variable (here, the current directory) contains whitespace or wildcards (\[?*), because the shell splits the result of variable expansions into words and performs globbing on these words. Always double-quote variable expansions "$foo" and command substitutions "$(foo)" (unless you specifically know you have not to).

In the general case, as other answers have mentioned already:

  • You can't use whitespace around the equal sign in an assignment: var=value, not var = value
  • The $ means “take the value of this variable”, so you don't use it when assigning: var=value, not $var=value.

Solution 4 - Bash

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.

Solution 5 - Bash

Here's your script...

DIR=$(pwd)
echo $DIR
while [ "$DIR" != "/" ]; do
    cd ..
    DIR=$(pwd)
    echo $DIR
done

Note the spaces, use of quotes, and $ signs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZenetView Question on Stackoverflow
Solution 1 - BashJohn WeldonView Answer on Stackoverflow
Solution 2 - BashJohannes WeissView Answer on Stackoverflow
Solution 3 - BashGilles 'SO- stop being evil'View Answer on Stackoverflow
Solution 4 - BashJimView Answer on Stackoverflow
Solution 5 - BashNikolas WolfeView Answer on Stackoverflow