taiyebur taiyebur - 6 months ago 24
Linux Question

Why is there extra output when using newline as IFS?

I was testing the output for different values of IFS using the code below.

#!/bin/bash

IFS=$'\n'

export $IFS

for i in $(ls -la);
do
echo $i;
done


But for newline as IFS the following things were also printed before the intended output. Why does it show this output?

declare -x DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-xwjTMGqSe7,guid=b8543bd3ba7dbc5ca284b0ce5741ccff"
declare -x DESKTOP_SESSION="default"
declare -x DISPLAY=":0"
declare -x GDMSESSION="default"
declare -x GDM_LANG="en_US.utf8"
declare -x GIO_LAUNCHED_DESKTOP_FILE="/usr/share/applications/atom.desktop"
declare -x GIO_LAUNCHED_DESKTOP_FILE_PID="19896"
declare -x GJS_DEBUG_OUTPUT="stderr"
declare -x GJS_DEBUG_TOPICS="JS ERROR;JS LOG"
declare -x GNOME_DESKTOP_SESSION_ID="this-is-deprecated"
declare -x GPG_AGENT_INFO="/run/user/1000/keyring/gpg:0:1"
declare -x LANG="en_US.utf8"
declare -x NODE_ENV="production"
declare -x NODE_PATH="/usr/share/atom/resources/app.asar/exports"
declare -x OLDPWD
declare -x PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_AGENT_PID="2218"
declare -x SSH_AUTH_SOCK="/run/user/1000/keyring/ssh"
declare -x WINDOWPATH="7"
declare -x XDG_CURRENT_DESKTOP="GNOME"
declare -x XDG_DATA_DIRS="/usr/share/gnome:/usr/local/share/:/usr/share/"
declare -x XDG_MENU_PREFIX="gnome-"
declare -x XDG_RUNTIME_DIR="/run/user/1000"
declare -x XDG_SEAT="seat0"
declare -x XDG_SESSION_DESKTOP="default"
declare -x XDG_SESSION_ID="1"
declare -x XDG_VTNR="7"

Answer

Because of the dollar sign in the export command.

Due to your assignment, $IFS will expand to a newline, which, without quotes, will simply be dropped by the shell, leaving the export command on its own with no arguments.

And according to help export:

export: export [-nf] [name[=value] ...] or export -p
  [...]
  If no NAMEs are given, or if `-p' is given, a list of
  all names that are exported in this shell is printed.
  [...]

Which is exactly what happens.

To fix that, just leave the export command off altogether, as it's not required (thanks, kojiro).