Setup:
File a
contains:
22
File b
contains:
12
I have shell script 1.sh
:
#!/bin/sh
a=$(< a)
b=$(< b)
echo $(($a*$b)) > c
The script should get values from file a
and b
, multiply them *
, and save to file c
.
However after setting permission $ chmod a+rx 1.sh
and running it $ ./1.sh
it returns an error:
./1.sh: 5: ./1.sh: arithmetic expression: expecting primary: "*"
This error occurs because the variables $a
and $b
doesn't get value form files a
and b
.
- If I
echo $a
andecho $b
it returns nothing; - If I define
a=22
andb=12
values in the script it works; - I also tried other ways of getting contents of files like
a=$(< 'a')
,a=$(< "a")
,a=$(< "~/a")
, and evena=$(< cat a)
. None of those worked.
Plot Twist:
However, if I change shebang line to #!/bin/bash
so that Bash shell is used - it works.
Question:
How to properly get data from file in sh?