简体   繁体   中英

Bash strips “N” character when using newline-delimiter

I am using Windows Linux Subsystem (Ubuntu). When I try to set a newline-delimiter, I lose my 'n'-characters. My simplified script;

#!/bin/sh
echo $HOME #gives /home/hennio
IFS=$'\n'
echo $HOME #gives /home/he  io

IFS=$'\\n\\b' didnt solve the problem. I checked my shebang with $(which sh), it is correct (although using zsh).

Searching on internet didnt give any results. Can someone please tell me whats going on? It driving me nuts..

To maintain compatibility, a shell invoked as /bin/sh usually tries to emulate a POSIX shell or some variant of a Bourne shell. Neither POSIX nor Bourne support $'...' . There are two possible solutions:

Method 1: Use the shebang of a shell, like bash , that supports $'...' .

Or,

Method 2: Use a POSIX method to assigning a newline to IFS :

IFS='
'

(Hat tip: Gordon Davisson )

Documentation

From man zsh :

Zsh tries to emulate sh or ksh when it is invoked as sh or ksh respectively

From man bash :

If bash is invoked with the name sh , it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM