1

I'm trying a very simple example of expr command to add a number to a variable. However, everytime I print it this is what I get :

Code :

MY=1

MY= expr $MY+1

// for some reason when i'm putting both the back ticks here, they disappear.

echo $MY

Output:

1+1

Why doesn't the output come as 2 in this case ? I've made sure those are back ticks and the spacing is right.

Also, when I use print instead of echo, it shows print doesn't exist.

1 Answer 1

1

You should add space to around +. Like this:

MY=`expr $MY + 1`

Because if you missed the space $MY+1, the shell will consider it as a string "1+1"

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

2 Comments

Thanks, that worked ! Also, I just realized if I don't put #!/bin/bash at the start for it to realize it is a script, it still works. Any idea why that happens ?
It always works. If you don't put "#!/bin/bash" at the start, the program will use the 'default' shell in your machine. Maybe the default shell is "/bin/sh". Putting "#!/bin/bash" will the your shell use correct bash instead of the default

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.