2

I have unclear information about word splitting in Shell and subshells.

Example:

IFS=""
file_name="file with space"
file_name=$(real_path $file_name)

Will $file_name get split in subshell? Or do I have to double-quote it like so:

file_name=$(real_path "$file_name")

After some testing I found out that:

  • if I set IFS=" ", then $file_name gets split by spaces
  • if IFS="" then whole $file_name is passed as first parameter.

ksh, dash, and bash all show this behavior so far.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
semtexzv
  • 129
  • 2
  • 10
  • 1
    forget the IFS stuff: a. echo "$*" concatenates all arguments as one argument, while b. echo "$@" wraps quotes around each blank-separated argument. the security wizards suggest using "${file_name}" to guarantee it's a single argument, regardless of blanks. IFS, while useful, shouldn't enter defense-against-blanks-in-file-name discussions. – Marty McGowan Apr 12 '15 at 00:44

1 Answers1

2

It will not word split since you set IFS to the empty string before forking the subshell, but you will still have to quote it to prevent pathname expansion.

that other guy
  • 116,971
  • 11
  • 170
  • 194