0

How to: a command result to a variable?

for file in find "$1" do
    var = $file | cut -d/ -f6-
    echo $var
    ...
done
1
  • A command result to a variable? variable=command (Where command is surrounded with `) Commented Nov 21, 2011 at 15:14

2 Answers 2

1

A few pointers:

In shell scripts whitespace really matters. Your code var = $file is an error, since no executable var exists which knows how to process the arguments = and $file. Use

var="$file"

Instead.

To capture the output of your $file | cut -d/ -f6- pipeline you will need the following syntax:

var="$(echo $file | cut -d/ -f6-)"

In bash of recent version you can use a "here string" instead and avoid the expense of echo and the pipe.

var="$(cut -d/ -f6- <<<"$file")"

I note that you are also attempting to process the results of a find command, also with incorrect syntax. The correct syntax for this is

while IFS= read -d $'\0' -r file ; do
    var="$(cut -d/ -f6- <<<"$file")"
    echo "$var"
done < <(find "$1")

I must again question you as to what "field 6" is doing, since you've asked a similar question before.

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

Comments

0

Your question wasn't all that clear, but is

var=`cut -d/ -f6- < $file`

what you were after?

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.