Skip to main content
deleted 3 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Building on a solution to one of many similar questions:

#!/bin/sh

IFS=+
printf '%s = %s\n' "$*" "$(($*))"
  • "$*" will expand to the positional parameters (the command line arguments), separated by $IFS. We set IFS to + separately.

  • "$(($*))" uses $(( ... )), which is an arithmetic substitution, to evaluate the arithmetic value of $*, the command line arguments with + in-between them.

  • printf is used to output the two strings with a = between them.

Testing:

$ ./script.sh 1 2 3
1+2+3 = 6
$ ./script.sh -1 2 3
-1+2+3 = 4
$ ./script.sh 1 2 3 4 2 2 3 1
1+2+3+4+2+2+3+1 = 18

What's missing in the script above is a verificationvalidation of the supplied command line arguments, to make sure they are integers. This is done for one value in the question "https://unix.stackexchange.com/questions/151654/checking-if-an-input-number-is-an-integer".

Building on a solution to one of many similar questions:

#!/bin/sh

IFS=+
printf '%s = %s\n' "$*" "$(($*))"
  • "$*" will expand to the positional parameters (the command line arguments), separated by $IFS. We set IFS to + separately.

  • "$(($*))" uses $(( ... )), which is an arithmetic substitution, to evaluate the arithmetic value of $*, the command line arguments with + in-between them.

  • printf is used to output the two strings with a = between them.

Testing:

$ ./script.sh 1 2 3
1+2+3 = 6
$ ./script.sh -1 2 3
-1+2+3 = 4
$ ./script.sh 1 2 3 4 2 2 3 1
1+2+3+4+2+2+3+1 = 18

What's missing in the script above is a verification of the supplied command line arguments, to make sure they are integers. This is done for one value in the question "https://unix.stackexchange.com/questions/151654/checking-if-an-input-number-is-an-integer".

Building on a solution to one of many similar questions:

#!/bin/sh

IFS=+
printf '%s = %s\n' "$*" "$(($*))"
  • "$*" will expand to the positional parameters (the command line arguments), separated by $IFS. We set IFS to + separately.

  • "$(($*))" uses $(( ... )), which is an arithmetic substitution, to evaluate the arithmetic value of $*, the command line arguments with + in-between them.

  • printf is used to output the two strings with a = between them.

Testing:

$ ./script.sh 1 2 3
1+2+3 = 6
$ ./script.sh -1 2 3
-1+2+3 = 4
$ ./script.sh 1 2 3 4 2 2 3 1
1+2+3+4+2+2+3+1 = 18

What's missing in the script above is a validation of the supplied command line arguments to make sure they are integers. This is done for one value in the question "https://unix.stackexchange.com/questions/151654/checking-if-an-input-number-is-an-integer".

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Building on a solution to one of many similar questions:

#!/bin/sh

IFS=+
printf '%s = %s\n' "$*" "$(($*))"
  • "$*" will expand to the positional parameters (the command line arguments), separated by $IFS. We set IFS to + separately.

  • "$(($*))" uses $(( ... )), which is an arithmetic substitution, to evaluate the arithmetic value of $*, the command line arguments with + in-between them.

  • printf is used to output the two strings with a = between them.

Testing:

$ ./script.sh 1 2 3
1+2+3 = 6
$ ./script.sh -1 2 3
-1+2+3 = 4
$ ./script.sh 1 2 3 4 2 2 3 1
1+2+3+4+2+2+3+1 = 18

What's missing in the script above is a verification of the supplied command line arguments, to make sure they are integers. This is done for one value in the question "https://unix.stackexchange.com/questions/151654/checking-if-an-input-number-is-an-integer".