0

I'm getting started with bash scripting and made this little script following along a short guide but for some reason when I run the script with sh myscript I get

myscript: 5: myscript: 0: not found running on ubuntu 12.04

here is my script below I should at least see the echo message if no args are set:

#!/bin/bash
#will do something

name=$1
username=$2
if (( $# == 0 ))
then
    echo "##############################"
    echo "myscript [arg1] [arg2]"
    echo "arg1 is your name"
    echo "and arg2 is your username"
fi

var1="Your name is ${name} and your username is ${username}"

`echo ${var1} > yourname.txt`

2 Answers 2

4
`echo ${var1} > yourname.txt`

Get rid of the backticks.

echo ${var1} > yourname.txt

...for some reason when I run the script with sh myscript...

Don't run it that way. Make the script executable and run it directly

chmod +x myscript
./script

(or run with bash myscript explicitly).

Sign up to request clarification or add additional context in comments.

3 Comments

The problem with the backticks is that they run the command and then the shell tries to run the output from that command as a command again. And sh is not bash. You should always use what you mean.
Thank you that works, as long as I run with ./ in front but if I just call it with myscript I get command not found but I can figure that out.
@A.Bal Without a path (and ./ is a path) the shell does a $PATH lookup for the name myscript and can't find it. If you want that to work you need to put it in a directory in your $PATH (or add a directory to your $PATH and put it in there).
1

It looks like that expression will work in bash but not in sh. As others pointed out change it to executable, make sure your shebang line is using bash and run it like this:

./myscript

If you want to run it with sh then it is complaining about line 5. Change it to this and it will work in /bin/sh.

if [ $# -ne 0 ]

Check out the man page for test.

Also you don't need the backticks on this line: echo ${var1} > yourname.txt

7 Comments

(( $# == 0 )) is perfectly valid shell test (try it). It is an arithmetic expression.
Yeah.. I tried it on the command line and it does work. I was able to replicate his error and I knew the other expression would work. Which is why I said I thought that was going on. I'd love to know what is happening there for myself as well.
Ok.. I see what's happening. That error only occurs if you run it with the /bin/sh shell. Running it with bash it works perfectly. Updating my answer to reflect that.
Right, sh is not bash.
But seems to be what OP is doing: "when I run the script with sh myscript"
|

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.