Questions tagged [ifs]

IFS is a variable in Unix shells (Bourne, POSIX sh, bash, ksh, …) that controls how unescaped substitutions are split into words.

IFS (short for "input field separator" and often referred to as "internal field separator") is a variable in Unix shells (, , , , , , , , …) that controls how strings are split into multiple fields. Most notably, variable substitutions (e.g. $foo) which are not inside double quotes or in a few other protected locations are split into multiple fields based on the value of IFS; the same goes for command substitutions (e.g. $(foo)). Each character that is present in IFS is a field separator. The default value contains a space, a tab and a newline.

For other meanings of the acronym IFS, use the appropriate tag:

  • IBM Integrated file system
  • QNX Image Filesystem
  • ERP by Industrial and Financial Systems AB
293 questions
138
votes
7 answers

What is the exact meaning of IFS=$'\n'?

If the following example, which sets the IFS environment variable to a line feed character... IFS=$'\n' What does the dollar sign mean exactly? What does it do in this specific case? Where can I read more on this specific usage (Google doesn't…
Yanick Girouard
  • 1,383
  • 2
  • 9
  • 4
67
votes
1 answer

What does IFS= do in this bash loop: `cat file | while IFS= read -r line; do ... done`

I'm learning bash and I saw this construction: cat file | while IFS= read -r line; do ... done Can anyone explain what IFS= does? I know it's input field separator, but why is it being set to nothing?
bodacydo
  • 75,521
  • 93
  • 229
  • 319
14
votes
2 answers

Trying to split a string into two variables

I'm trying to split a string into two variables (without having to use a while loop): var="hello:world" IFS=':' read var1 var2 <<< $var echo "var1: $var1" echo "var2: $var2" but i'm not getting the desired result: var1: 'hello world' var2:…
coda
  • 622
  • 1
  • 7
  • 14
12
votes
1 answer

IFS=: leads to different behavior while looping over colon-separated values

Experiment 1 Here is my first script in file named foo.sh. IFS=: for i in foo:bar:baz do echo $i done This produces the following output. $ bash foo.sh foo bar baz Experiment 2 This is my second script. IFS=: for i in foo:bar:baz do unset…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
12
votes
3 answers

Surprising array expansion behaviour

I've been surprised with the line marked (!!) in the following example: log1 () { echo $@; } log2 () { echo "$@"; } X=(a b) IFS='|' echo ${X[@]} # prints a b echo "${X[@]}" # prints a b echo ${X[*]} # prints a b echo "${X[*]}" # prints…
Norswap
  • 11,740
  • 12
  • 47
  • 60
8
votes
3 answers

How does one properly assign temporary Bash variables on a per-command basis?

Bash seems to behave unpredictably in regards to temporary, per-command variable assignment, specifically with IFS. I often assign IFS to a temporary value in conjunction with the read command. I would like to use the same mechanic to tailor output,…
vintnes
  • 2,014
  • 7
  • 16
8
votes
3 answers

Setting IFS to null byte does not split lines correctly in command line

~ ls A B C On bash (looks wrong) ~IFS=$'\x00' read -a vars < <(find -type f -print0); echo "${vars}" ABC On zsh (looks good) ~IFS=$'\x00' read -A vars < <(find -type f -print0); echo "${vars}" A B C Is it a bash bug?
Pan Ruochen
  • 1,990
  • 6
  • 23
  • 34
8
votes
1 answer

Word splitting in Bash with IFS set to a non-whitespace character

I'm going through a Bash tutorial, and specifically the subject of word splitting. This script, called "args", helps demonstrate word splitting examples: #!/usr/bin/env bash printf "%d args:" $# printf " <%s>" "$@" echo An example: $ ./args hello…
Nadim Hussami
  • 349
  • 2
  • 14
7
votes
2 answers

Concat array elements with comma and single quote - Bash

How to convert array elements with single quotes and comma in Bash. arr=("element1" "element2" "element3") #element1 element2 element3 Desired result 'element1','element2','element3' From Martin Clayton answer comma seprated values are achieved…
Ishpreet
  • 5,230
  • 2
  • 19
  • 35
7
votes
2 answers

When do I set IFS to a newline in Bash?

I thought setting IFS to $'\n' would help me in reading an entire file into an array, as in: IFS=$'\n' read -r -a array < file However, the above command only reads the first line of the file into the first element of the array, and nothing…
learningbee
  • 333
  • 1
  • 5
  • 11
7
votes
4 answers

shell - temp IFS as newline only. Why doesn't this work: IFS=$(echo -e '\n')

I'm trying to use for in the shell to iterate over filenames with spaces. I read in a stackoverflow question that IFS=$'\n' could be used and that seems to work fine. I then read in a comment to one of the answers that to make it posix compatible…
user2672807
  • 1
  • 2
  • 7
  • 20
7
votes
3 answers

bash: reading text from a string one character at a time, with whitespace

I'm bored, and decided to write a script for a text-based adventure of mine using bash. Basically, it's supposed to animate a typewriter in certain cases for dramatic storytelling. I can do this manually in a file like so: sleep 0.1 echo -n…
derp
  • 411
  • 2
  • 5
  • 12
6
votes
3 answers

How to split break lines with Bash scripts?

If you have a string with a delimiter, let's say a , character, you could use IFS just like that: text=some,comma,separated,text IFS="," read -ra ADDR <<< "$text" for i in ${ADDR[@]} do echo $i done Each word will be printed in a new line. But…
Victor Ferreira
  • 6,151
  • 13
  • 64
  • 120
6
votes
2 answers

zsh is not splitting by IFS after parameter expansion

This is my code to loop over colon separated values and do something with each value. f() { IFS=: for arg in $1 do echo arg: $arg done } f foo:bar:baz This works fine in most POSIX compliant shells. $ dash foo.sh arg:…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
5
votes
1 answer

Why doesn't while ''IFS=, read -r part'' split a comma-separated string into two parts?

since the default value of the IFS variable is a newline/tab/space, the following code: while read -r line do echo $line done <<< "hello\nworld" outputs: hello world but, if I understand it correctly, I can change it so it will separate it…
yoyobara
  • 101
  • 1
  • 5
1
2 3
19 20