4

I wrote following line in my script , but it not assigning any value to variable a

a= grep -n mark /etc/samba/smb.conf |cut -d: f1

I aspect line number where mark string present in smb.conf in variable a. But value of variable is null. What may be the issue

5 Answers 5

10

Two things:

  1. There can be no spaces between name of the variable, the = signal and the value of the variable
  2. If you want to put the results of a command or series of commands on a variable, you must enclose it on $(command) or old style backticks. Both lines below are correct and do the same thing:

    a=`commands`
    a=$(commands)
    

So your line must be

a=$(grep -n mark /etc/samba/smb.conf |cut -d: f1)

There are some differences on how to concatenate the use of backticks or new style $(), check this question on SO to see a good response.

1
  • +1. I recommend putting the delimiter character in single quotes, too: -d':'. It's not needed here, but it's a good habit to have. Mar 9, 2011 at 13:06
2
a=`grep -n mark /etc/samba/smb.conf |cut -d: -f1`
2

Try this instead

a=$(grep -n mark /etc/samba/smb.conf |cut -d: f1)

Or instead of the $() you could use backticks (which here turn on the nice code look)

1

Should be a=$( ... ) or

a=` … `
1

Maybe by trying to use cut like this:

a=`grep -n mark /etc/samba/smb.conf |cut -d: -f1`

You must log in to answer this question.

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