0

I have something like this in bash on Linux:

some_variable= ls *somepattern* | xargs cat | wc -c

And i would like to multiply that by some amount like:

another_variable = $(($some_variable * 10))

But i get an error

  -bash: * 100: syntax error: operand expected (error token is "* 100")

Why is it not possible to not multiply some_variable with wc?

2
  • 2
    Run your script through shellcheck at shellcheck.net. As posted, it contains several errors. Also, which shell are you in? You probably need a shebang, even if only to let shellcheck know what syntax applies. I can't figure why the error message shows a number 100 which does not appear in your commands. Commented Jul 9, 2020 at 12:33
  • shellcheck came up with seven errors on those two lines. It will save you (and us) a lot of time in future. Commented Jul 9, 2020 at 14:27

1 Answer 1

5

You never get to the multiplication, you aren't actually assigning a variable there. You can't have a space around the =, you need:

some_variable=some_value

Next, if you want to assign a variable to the output of a command, you ned to use command substitution:

some_variable=$(some_command)

Or, the still supported but becoming deprecated backticks:

some_variable=`some_command`

So, what you wanted was:

some_variable=$(ls *somepattern* | xargs cat | wc -c)

But that would be much better done like this:

some_variable=$(cat *somepattern* | wc -c)

Once you have that, you can do:

another_variable=$(($some_variable * 10))

Finally, your error was caused because, as explained in the first section, your variable was empty, so you ended up running this:

$ another_variable = $(( * 10))
bash: * 10: syntax error: operand expected (error token is "* 10")
1
  • 1
    Also notice multiplying by 10 will have the same effect of another_variable=${some_variable}0. Commented Jul 9, 2020 at 13:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.