In Bash, I need an integer variable, like this:
declare -i n_jobs
to be assigned as a value the number of current background jobs:
jobs | wc -l
If I assign it as so:
n_jobs=$(jobs | wc -l)
is seems like a working integer, e.g.:
echo $((++n_jobs))
but... printing it (without running the increment above) reminds me that it contains blanks:
$ echo "$n_jobs"
$ <space><space><space><space><space>4
so I resort to this construct:
n_jobs=$(( $(jobs | wc -l) ))
to force immediate "casting" to int.
Is there a better way to take the output of a command substitution list and assign it to a variable as an integer?
$ echo $n_jobsInstead of$ echo "$n_jobs"x=7+9works, butx=3*(7+9)still produces a syntax error and needs to be written asx=$(( 3*(7+9) )). A simple comment the first time you assign to a variable stating# n_jobs is an integerwould work just as well. Also, I cannot reproduce the spaces you are observing.