27 Dec 2022
I have been meaning for a few weeks to write a brief tip about the IFS (Internal File Seperator). Granted, mine was going to cover
how the IFS is affected by sub-shells (including the less-obvious examples such as while
loops), but the ChatGPT did a pretty good
job of writing the basics in less than one second!
This is what it wrote (the block below is ChatGPT-generated content, not mine!):
The Internal Field Separator (IFS) is a special shell variable in Bash that determines how Bash splits words and fields. It is used when Bash reads a command or script, and it controls how Bash interprets spaces, tabs, and newline characters. By default, the IFS is set to a space, a tab, and a newline character. This means that when Bash reads a command or script, it will split the input into words wherever it encounters a space, a tab, or a newline character. You can change the value of the IFS variable to customize how Bash splits words and fields. For example, you might set the IFS to a colon (:) if you want to split input on colon characters instead of spaces or tabs. Here's an example of how you might use the IFS variable in a Bash script: # Set the IFS to a comma IFS=',' # Read a line of input from the user read -r field1 field2 field3 # Split the input on commas echo "Field 1: $field1" echo "Field 2: $field2" echo "Field 3: $field3" In this example, the IFS is set to a comma (,) before the user is prompted to enter input. When the user enters a line of input, Bash will split the input on commas and assign the resulting fields to the variables field1, field2, and field3. The echo commands will then print the values of these variables to the console.
I will get around to expounding further, in the meantime my ancient blog has a bit more information!
My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.
![]() Shell Scripting Tutorial is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion. Also available in PDF form from Gumroad:Get this tutorial as a PDF | ![]() Shell Scripting: Expert Recipes for Linux, Bash and more is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script. |