2

I've read bash manpage and other docs, but I find the explanation a bit confusing.

I have the following simple script:

#!/bin/bash

IFS=""

echo $2

var=A B C

echo $var

if I execute it, eg:

LANG=C ./args2 3 4
4
./args2: line 7: B: command not found

I really don't understand, why $2 = 4? If manual says:

If the value of IFS is null, no word splitting occurs.

The same thing with var declaration, why it complains about "B"? So there is word splitting anyway?

1

This section of the bash manual lays out the steps: Shell Operation

In step 3, the script is broken up into commands, arguments, etc.

  • in this step, we get the tokens "var=A", "B" and "C"
  • see section Simple Commands where it says (emphasis mine):

    A simple command is [...] just a sequence of words separated by blanks

Word splitting is not performed until expansions, step 4.

As Qudit explained, your shell split "LANG=C ./args2 3 4" into tokens before launching the command "./args2"

1

Before your script runs, IFS has its default value. The argument splitting is done in the bash instance in which you ran your script and it splits things normally because you haven't set IFS yet.

  • If I run on the very shell: export IFS=""; var=A B C bash: B: no se encontró la orden. It does the same thing. Why? – sebelk May 3 '15 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.