The IFS variable is used in as the input field separator. If you set IFS to | (i.e. IFS=| ), | will be treated as delimiters between words/fields when splitting a line of input. [donotprint]
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | None |
Time | 2m |
cat -etv
Sample outputs:
^I$ $
In this example, read first and last name using read command and set IFS to a white space:
IFS=' ' read -p 'Enter your first and last name : ' first last echo "Hello, $first $last"
Sample outputs:
Enter your first and last name : Vivek Gite Hello, Vivek Gite
In this example set IFS to | and read data:
IFS='|' read domain ip4 ip6 <<< 'google.com|74.125.236.65|2404:6800:4007:801::1008' echo "$domain has $ip4 IPv4 and $ipv6 IPv6 address."
Sample outputs:
google.com has 74.125.236.65 IPv4 and IPv6 address.
while loop example with IFS and read command
Create a text file (named foo.txt) as follows:
$ cat foo.txt
Sample outputs:
google.com|74.125.236.65|2404:6800:4007:801::1008 i.theos.in|58.27.86.81|2600:807:320:305::3f6e:f648 cyberciti.biz|75.126.153.206|2600:807:320:305::3f6e:f649
Create a bash shell script as follows:
#!/bin/bash _input="foo.txt" # set IFS (internal field separator) to | # read file using while loop while IFS='|' read -r domain ip4 ip6 do echo "$domain has $ip4 IPv4 and $ip6 IPv6 address." done < "$_input"
🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.
🐧 7 comments so far... add one ↓
🐧 7 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Thx Vivek, i use custom IFS sometimes when i do bash scripts, but i’ve never had the idea to include it directly inside the while loop !
That easy, quick, efficient and class, just what i like.
Thx for the tips.
Pierre B.
Hi Vivek,
I did try to apply this trick to one of my script, but it seems that specifying the IFS this way broke something, when i put it back as it was before : it works again.
Here is the function where i do use the custom IFS :
Ok, i finally change that piece of *** i posted for something cleaner, and then it works.
Just in case i post it there :
Just a side note:
with cat, both -e and -t specifies -v implicitly.
So you’re actually specifying -v three times.
cat -etv = cat -et
There is a typo in this example, ip6 will not give an output since you define $ipv6
Thanks for the heads up!
Dear ViVek,
Thank you ! \o/