Home > Enterprise >  This loop will only ever run once. Bad quoting or missing glob/expansion?
This loop will only ever run once. Bad quoting or missing glob/expansion?

Time:12-10

Im working on a bash script that recive a list of processes and do a bunch of things with them, however when I want to analyze them with a loop this error happens. Here is some code:

#! /bin/bash 

ls /proc | grep ^9 > processes.txt 

cat processes.txt 

for line in $processes.txt
do
    echo "$line"
done

PD: Im preatty new to bash

CodePudding user response:

$ does parameter expansion; it does not expand a file name to the contents of the file.

Use a while read loop instead.

while IFS= read -r line; do
    echo "$line"
done < processes.txt
  •  Tags:  
  • bash
  • Related