Hey everyone! I'm new to bash scripting and I'm trying to use the cowsay command in a simple script. The script reads user input and outputs it with cowsay, but it fails at that line with a "command not found" error. I've already added cowsay to my PATH because I can run it from the terminal without any issues. Any help on how to get it to work in my script? Thanks!
5 Answers
Try adding this line at the top of your script to see the PATH it runs with: `echo $PATH`. It might be different from your terminal’s PATH, causing issues with locating cowsay.
`echo $PATH` returns `/home/cj/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin`, but `echo $BASH_ENV` is blank.
Make sure you're not using `exec cowsay` in your script unless you know what that does. Just calling cowsay directly should work if it's in your PATH.
I’m not really sure how to use exec. Can you explain?
It looks like you need to share more details. Can you post your script? If cowsay is in your PATH, there might be a typo causing the issue.
Here's the whole script: `#!/bin/bash` `read moo` `cowsay $moo`. The error I'm seeing is `bash: cowsay: No such file or directory`.
When running your script, try `which cowsay` in your terminal. Then use the returned path directly in your script instead of just `cowsay`.
You should check that your script has execute permissions. Use this command: `chmod +x name-of-script`. When you run it, make sure to use `./name-of-script` to execute it from your current directory.
But he mentioned the script runs fine until the cowsay line.
You can also run `which cowsay` to find the full path to it, then use that full path in your script.