Sphenxes Sphinx
Sphenxes Sphinx

Reputation: 5

How to save the result of grep to variable x?

#!/bin/bash

for i in *.pdf; do

    echo $i
    x= pdfinfo "$i" | grep "Title" # nothing is stored in variable x
    echo $x

    if [ ! -z $x ]; then
        echo $x # print null
        cp $i "$x"
    fi
done

Nothing is stored in variable x — but why not and how do I do it.

Upvotes: 0

Views: 149

Answers (2)

user1428716
user1428716

Reputation: 2136

You could use:

x=`pdfinfo "$i" | grep "Title"`

Upvotes: 1

Maxime Chéramy
Maxime Chéramy

Reputation: 18821

Add parentheses or backquotes:

x=$(pdfinfo "$i" | grep "Title")

or

x=`pdfinfo "$i" | grep "Title"`

Note that the latter solution should be avoided now, it is the historical way of doing it and is replaced by $(...). The $(...) solution is more readable in particular in case of nested substitutions.

Upvotes: 2

Related Questions