i have this bash line

current_part= `echo "${lines[$i]}"  | cut -d , -f$x`

is it possible to use variable $x with cut's parameter -f ? i want -f1, -f2, -f3 and so on with each iteration of $x


Yes it is possible:

$ lines=("abc,def" "foo,bar")

$ x=1
$ echo "${lines[1]}" | cut -d, -f$x

$ x=2
$ echo "${lines[1]}" | cut -d, -f$x

However this sounds like an XY problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.