Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I pass the $line to the cut command properly in this loop?

while read line
    login= $(cut -d : -f 1)

done < /etc/passwd

I can't do $(cut -d : -f 1 $line) so what is the correct way?

share|improve this question
up vote 1 down vote accepted

You don't actually need the while loop if your intention is only to list the names. Also there is a syntax error after login=, there should be no space.

cut -d: -f1 /etc/passwd | \
while read login; 
    echo username: $login;

or as you tried:

while read line; do
   login=$(echo $line | cut -d : -f 1)
   echo $login
done < /etc/passwd

even better:

db-getent passwd |cut -d: -f1 | xargs -L1 echo name:
share|improve this answer
+1 for getent. In your first example, line continuations are not required after a pipe. – glenn jackman Mar 11 '13 at 1:14
I didn't realize, thanks! – Ярослав Рахматуллин Mar 11 '13 at 8:55
Perfect! Thanks Ярослав, the loop was a snippet of code from a larger while loop so your second suggestion is perfect for me. – Mike Mar 11 '13 at 17:54

Let the read command together with the shell IFS variable parse the line for you:

while IFS=: read -r login restOfLine; do
    doSomethingWith $login
done < /etc/passwd

To answer your question, the bash here-string would be useful:

login=$(cut -d: -f1 <<< "$line")
share|improve this answer
The here-string is nice and tidy I like that! Cheers. – Mike Mar 11 '13 at 18:01

Use echo:

login=$(echo "$line" | cut -d : -f 1)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.