0

How can I save the output of this command into a var and still remove the files?

rm -vri files | wc -l
  • Possible duplicate of [How to set a variable equal to the output from a command in Bash?](http://stackoverflow.com/questions/4651437/how-to-set-a-variable-equal-to-the-output-from-a-command-in-bash) – Benjamin W. Mar 30 '16 at 01:46

1 Answers1

1
$ touch file1 file2
$ out=$(rm -vri file1 file2 | wc -l)
rm: remove regular empty file ‘file1’? y
rm: remove regular empty file ‘file2’? y
$ echo $out
2

As you can see, using the normal var=$(command) method works.

This also works when running it from a script. Obviously, you do need to provide the input (e.g. running it from cron won't work).

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • Use quotes: `echo "${out}"`, for instance when your output has more than one line. – Walter A Mar 29 '16 at 17:19
  • @Carpetsmoker I know that you know, but Carlos might not. And he might want to use your solution in other situations, like I did once. – Walter A Mar 29 '16 at 17:23
  • @WalterA yup thats not the case here, i want to run an option in a bash script wich can remove the files and saves the number of how many files have been erased so i can increment that number in another var! But thanks anyway, I was not aware of that option! I'm learning bash at university so yeap, pretty noob yet! – Carlos M. Silva Mar 29 '16 at 17:31