0

I have the following bash script thowring exception: "cut: option requires an argument -- 'f'".

It would cut out the 5th colunm from /etc/passwd, separated by ':' character.

How should I pass the $variable to the option's parameter?

variable=5; cut -d':' -f$variable < /etc/passwd
2
  • 4
    That line of code works fine for me. Commented Mar 5, 2018 at 21:14
  • 1
    this should work. make sure you're on bash shell. echo $0 will say. Also same as this variable=5; cut -d: -f$variable /etc/passwd with extra chars eliminated. Commented Mar 5, 2018 at 21:17

2 Answers 2

1

You get this when $variable is empty:

$ variable=
$ cut -d: -f$variable </etc/passwd
cut: option requires an argument -- 'f'
Try 'cut --help' for more information.

2 suggestions:

  1. ensure the variable has a value
  2. quote your variables, and in this case, separate the option from the argument: at least you'll get a saner error when the value is empty

    $ cut -d : -f "$variable" </etc/passwd
    cut: fields are numbered from 1
    Try 'cut --help' for more information.
    
Sign up to request clarification or add additional context in comments.

Comments

0

To avoid that, use

set -o nounset

and then, if variable is unset you'll receive

bash: variable: unbound variable

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.