The following example is taken from William Shott's The Linux command line. It is a shell script which gathers user info from the /etc/passwd file. The specific user for which this is done is read from stdin.
My question is about the "here string" used in line 7. Just for kicks, I tried using the redirection operator instead, but it did not work. Why?
PS: I have some background in C++ so I expected the string file_info
to act as a stringstream.
1 #!/bin/bash
2 # read-ifs: read fields from a file
3 FILE=/etc/passwd
4 read -p "Enter a username > " user_name
5 file_info="$(grep "^$user_name:" $FILE)"
6 if [ -n "$file_info" ]; then
7 IFS=":" read user pw uid gid name home shell <<< "$file_info"
8 echo "User = '$user'"
9 echo "UID = '$uid'"
10 echo "GID = '$gid'"
11 echo "Full Name = '$name'"
12 echo "Home Dir. = '$home'"
13 echo "Shell = '$shell'"
14 else
15 echo "No such user '$user_name'" >&2
16 exit 1
17 fi
If you do something like
the right side of the pipeline (the
read
command) is run in its own subshell process and the variables it sets are gone when it ends soecho
just displays empty strings.But you can do something like
In this case the whole
(
...)
part is run in the same subshell and the variables are available for theecho
call.