1

Assume let the value of the variable a be 42,

b=18+$a
The value of b should be 60

But I'm not getting the value 60. Instead it's printing 18+42. How can i do it ?

New Query :

grep -F "$name" -A1000 filename | sed -n '1p;19p;24p'

Assume let a=10,b=20,c=30.In the above grep command can i use '$ap;$bp;$cp' instead of '1p;19p;24p' ?

Another thing, I've given as -A1000. Which implies that starting from 1p it considers till 1000 line , right ? I need to search throughout file without giving the number. Ho

0

2 Answers 2

6

Bash:

b=$((18+a))
echo "value of b is $b"

or

let b=18+a
echo "value of b is $b"
Sign up to request clarification or add additional context in comments.

11 Comments

@ Prince : Thank U. By the way do u remember this grep -F "$name" -A37 filename| sed -n '1p;19p;24p' ;which u've guided me yesterday ? In the above command can i mention '$ap;$bp;$cp' instead of '1p;19p;24p' ?
Inside $(()) and on the right side of a let statement the use of the dollar sign is not required. E.g. let b=18+a would work just as well.
@JackieJames: Variables don't expand inside single-quoted strings. In addition, bash would not see "A variable named a followed by the letter p," for example, instead i would see "A variable named ap," which is not what you mean. You should double-quote the expansion and include curly braces for clarity: "${a}p;${b}p;${c}p"
@ Prince : grep -F "$name" -A1000 filename | sed -n '1p;19p;24p' Assume let a=10,b=20,c=30.In the above grep command can i use '$ap;$bp;$cp' instead of '1p;19p;24p' ? Another thing, I've given as -A1000. Which implies that starting from 1p it considers till 1000 line , right ? I need to search throughout file without giving the number.
@JackieJames: use double quotes
|
1

Any mathematical operation in Bash involves the use of $(())

So, for addition, you would do :-

b=$((18 + a))

Notice that the '$' before a is not required. Some more examples of mathematical operations in Bash are :-

a=$((17+1))
b=$((100-a))
c=$((a*b))
d=$((c/b))
echo $((10 * 1024 * 1024)) # Echoes the number bytes in 10 MB

Comments

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.