0

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"
1
  • You probably want to read Bash FAQ 001. Commented May 23, 2016 at 12:57

1 Answer 1

3

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 in this case (thanks, kojiro).

Sign up to request clarification or add additional context in comments.

3 Comments

This is a correct answer to the question asked, but I don't think OP actually needs to export IFS at all.
@kojiro, so it would seem.
@kojiro You're right, it's not required here. Thanks for spotting that. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.