Search code examples
bashshellstdincut

Passing line to cut and assigning to variable (Bash)


I am able to read in and print every first name like so:

while read input;
do
    echo $input | cut -d ',' -f 1
done

but if I try and assign it to a variable and print it does not work. it just outputs three blank lines. (there are only three records in my file). I tried other methods as well but I attached my most recent attempt.

while read input;
do
    firstName=$input | cut -d ',' -f 1
    echo $firstName
done

This is what my user list looks like:

Jet,Black
Faye,Valentine
Radical,Edward

Solution

  • Figure it out. I had tried this solution before but forgot the echo in front of $input.

    while read input;
    do
        firstName=$(echo $input | cut -d ',' -f 1)
        echo $firstName
    done