0
        #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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.