2

There is a quite nasty expression that want to echo using bash.

The expression is:

'one two -- 

Note: There is white space after --.

So I have:

IFS=
echo 'one$IFStwoIFS--$IFS

But the result is:

one$IFStwo$IFS--$IFS
yasin
  • 107
  • 10

1 Answers1

3

You have few issues with your approach:

  1. Within single quote variables are not expanded in shell
  2. In the string one$IFStwo$IFS--$IFS first instance of $IFS will not be expanded since you have string two next to $IFS so it attempts to expand non-existent variable $IFStwo.
  3. Default value of $IFS is $' \t\n'

You can use:

echo "one${IFS}two$IFS--$IFS"

which will expand to (cat -A output):

one ^I$
two ^I$
-- ^I$
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Try this: `( IFS=' ' && echo ""; )` – anubhava Jun 02 '18 at 10:08
  • good... this worked: `IFS=' ';echo$IFS"'or${IFS}true$IFS--$IFS"`. pardon. If i replace something else with `IFS=' ' `. I mean I want to replace something else with whitespace in `IFS=' '`. What should I do/ – yasin Jun 02 '18 at 10:13
  • Didn't get but `( IFS=':'; echo ""; )` also works fine. – anubhava Jun 02 '18 at 10:15
  • I need a character else instead of whitespace but working as whitespace... when I run bash I need to hide whitespace – yasin Jun 02 '18 at 10:17
  • Really thank you for supporting me. I mean what special character I can use in `IFS=' '` instead of whitespace? – yasin Jun 02 '18 at 10:22
  • I have `IFS=':'` which is not same as `IFS=''` . May be you mean something else but it is not clear from comments. I suggest you edit your question and clarify – anubhava Jun 02 '18 at 10:24
  • I mean some methods like this: `IFS=$'\x20';echo$IFS"'first${IFS}second$IFS--$IFS"`. any other idea? :) – yasin Jun 02 '18 at 10:45
  • `( IFS=$'\x20' && echo$IFS""; )` works fine for me – anubhava Jun 02 '18 at 11:15