results of wc as variables

In pure bash: (no awk) a=($(wc file.txt)) lines=${a[0]} words=${a[1]} chars=${a[2]} This works by using bash’s arrays. a=(1 2 3) creates an array with elements 1, 2 and 3. We can then access separate elements with the ${a[indice]} syntax. Alternative: (based on gonvaled solution) read lines words chars <<< $(wc x) Or in sh: a=$(wc file.txt) … Read more

bash echo number of lines of file given in a bash variable without the file name

An Example Using Your Own Data You can avoid having your filename embedded in the NUMOFLINES variable by using redirection from JAVA_TAGS_FILE, rather than passing the filename as an argument to wc. For example: NUMOFLINES=$(wc -l < “$JAVA_TAGS_FILE”) Explanation: Use Pipes or Redirection to Avoid Filenames in Output The wc utility will not print the … Read more

tech