3

I don't know how to use variables for further executing in a script.

I tried the following:

#!/bin/bash
NUM = 0
echo Number $NUM > text.txt

but I get the following error:

num.sh: 3: num.sh: NUM: not found

3 Answers 3

13

There must not be any whitespace around = in variable declaration in shell.

Remove the whitespaces:

NUM=0

Also if you don't have any good reason, don't use all uppercases for a user defined shell variable name as there is chance that this could conflict with any environment variable.

Better do:

number=0
1
  • 6
    +1 for lower case variables. I've heard seasoned programmers - people much smarter than I am - saying that bash variables should always be upper case...
    – pfnuesel
    Nov 5, 2016 at 1:28
3

Don't use spaces in line NUM=0

2

You may need to remove the space after NUM , so the script should look like:

#!/bin/bash
NUM=0
echo $NUM > text.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .