Bla bla
Bla bla

Reputation: 111

Bash setting variable with command value

        #echo $LINE |cut -f"${arg}" -d' '
    pom=$LINE |cut -f"${arg}" -d' '

I have this two lane. First is working but second not. I want to variable get a value of this command cuz i want use this value like string.

Upvotes: 0

Views: 153

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185025

This kind of code is discouraged :

pom=`echo $LINE |cut -f"${arg}" -d' '`

in favor of :

pom=$(echo "$LINE" | cut -f"${arg}" -d' ')

The backquote () is used in the old-style command substitution, e.g. foo=command`. The foo=$(command) syntax is recommended instead. Backslash handling inside $() is less surprising, and $() is easier to nest. See http://mywiki.wooledge.org/BashFAQ/082

Upvotes: 2

choroba
choroba

Reputation: 241828

If there are no empty fields in LINE (i.e. no repeated spaces), you can also use pure bash:

ITEMS=($LINE)
pom=${ITEMS[arg]}

Note that $arg is zero based in this case, so you might need to use [arg-1].

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409166

You need to run the first line, then assign the value returned to the variable. You do this with the command inside backticks, like this:

pom=`echo $LINE |cut -f"${arg}" -d' '`

The reason the second line doesn't work, is because whats in $LINE is most probably not a valid command, and pipes takes outputs from commands, which is why you need echo to output the contents of $LINE.

Upvotes: 3

Related Questions