function::int_arg - Linux


int_arg is a command used for extracting integer arguments from a string. Its primary application is in scenarios where a script or program requires integer values from a user or an external source and needs a reliable way to extract and validate those values.


int_arg [-h] [-d DELIM] [-e ERRSTR] [-o OFFSET] [-r] [-s STR]


  • -h, –help – Displays help information.
  • -d, –delim DELIM – Specifies the delimiter to use for extracting the integer. Default is a whitespace character.
  • -e, –errstr ERRSTR – Customizes the error message displayed in case of invalid integer values.
  • -o, –offset OFFSET – Specifies the offset from the start of the string to begin extraction. Default is 0.
  • -r, –required – Marks the argument as required, resulting in an error if no valid integer is found.
  • -s, –str STR – Input string from which to extract the integer.


Extract an integer from user input:

read str
int_arg -s "$str"

Extract an integer from a string, using a custom delimiter:

str="num 10 int"
int_arg -s "$str" -d ":"

Extract an integer, prompting for input if missing:

int_arg -r

Common Issues

  • Invalid integer value: If no valid integer can be extracted, the command will return an error. Use the -e flag to customize the error message.
  • No input provided: In case of missing input for mandatory arguments, the command will return an error. Use the -r flag to enforce required arguments.


Combine with other commands:

echo "$mystr" | int_arg -d " "

Use in scripts:

input="value 10"
num=$(int_arg -s "$input" -d " ")
echo "Extracted integer: $num"

Related Commands

  • extract_int – Extracts integer values from JSON or YAML data.
  • expr – Evaluates arithmetic expressions, including integer operations.
  • bc – Arbitrary-precision calculator, allowing for integer manipulation.