1

I want to do some simple calculations with variables in an script.sh, in the same directory there is a folder called New Folder2. So, I want to enter into the folder and save a text file.

The script is the following:

#!/bin/bash
foldername='New Folder'
number=1
number=$number+1
cd $foldername$number
echo '$number' > number.txt

But then I only get the file number.txt in the same directory ./ with 1+1 not the actual sum. What should I do?

2 Answers 2

1

For integers, use arithmetic expansion: $(( EXPR ))

num=$((num1 + num2))
num=$((num + 1))
3
  • It only works for integers? And what can I do for other types of variables? Nov 17, 2016 at 15:57
  • 1
    @JoshuaSalazar you can use your way with strings a='hello' b='world' c=$a$b echo $c > helloworld Nov 17, 2016 at 16:00
  • 1
    To concatenate/"add" strings you can just do something like $string="$string$anotherstring". Also, an alternative to the arithmetic expansion would be to use let number=$number+1, though I think the $(( )) syntax is more easily readable..
    – IanC
    Nov 17, 2016 at 16:02
1

Look at 4th and 5th line!

#!/bin/bash
foldername='New Folder'
number=1
number=$((number+1))
cd "$foldername$number"
echo '$number' > number.txt

You must log in to answer this question.

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