How to Run Cowsay from a Bash Script?

0
5
Asked By CuriousCoder42 On

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

Answered By DevGuru29 On

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.

TechLover_21 -

You can also run `which cowsay` to find the full path to it, then use that full path in your script.

CuriousCoder42 -

`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.

Answered By ScriptingNinja33 On

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.

CuriousCoder42 -

I’m not really sure how to use exec. Can you explain?

Answered By ShellMaster87 On

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.

CuriousCoder42 -

Here's the whole script: `#!/bin/bash` `read moo` `cowsay $moo`. The error I'm seeing is `bash: cowsay: No such file or directory`.

Answered By TerminalWizard54 On

When running your script, try `which cowsay` in your terminal. Then use the returned path directly in your script instead of just `cowsay`.

Answered By BashWhizKid On

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.

CleverUser99 -

But he mentioned the script runs fine until the cowsay line.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.