I made a basic bash script piping fortune and cowsay to make a wise cow, and titled it appropriately as wisecow Is there a way I can enable the flags associated with cowsay so that I can input

wisecow -s

and get the stoner cow, or -b for the borg cow, or what have you?

Currently when I do

wisecow -d

or whatever, the cow looks the same as if I hadn't used the flag

the code lol:

fortune | cowsay
  • You can use the bash builtin command getopts so that your script can process its arguments and you can pass them on to whichever command. Search here and Stack Overflow for getopts, there are loads of examples. – glenn jackman Oct 25 '19 at 2:04
  • thank you. would I just put that in a new line on the script? – gwyns Oct 25 '19 at 2:11
  • Ahhh, thank you. I forgot to put it in when I rewrote the post, I'll pop it in. That fixed it, thank you very much @steeldriver – gwyns Oct 25 '19 at 3:44

You give some option to your script, but you never pass it on to cowsay.

To do that, use

fortune | cowsay "$@"

in your script. The "$@" will expand to the list of command line arguments that your script was given, for example the option -d, if that was what you invoked your script with.

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.