Split bash string by newline characters

前端 未结 2 1491
不知归路
不知归路 2020-12-12 19:16

I found this.

And I am trying this:

x=\'some
   thing\'

y=(${x//\\n/})

And I had no luck, I thought it could work with double back

相关标签:
2条回答
  • 2020-12-12 19:25

    There is another way if all you want is the text up to the first line feed:

    x='some
    thing'
    
    y=${x%$'\n'*}
    

    After that y will contain some and nothing else (no line feed).

    What is happening here?

    We perform a parameter expansion substring removal (${PARAMETER%PATTERN}) for the shortest match up to the first ANSI C line feed ($'\n') and drop everything that follows (*).

    0 讨论(0)
  • 2020-12-12 19:35

    Another way:

    x=$'Some\nstring'
    readarray -t y <<<"$x"
    

    Or, if you don't have bash 4, the bash 3.2 equivalent:

    IFS=$'\n' read -rd '' -a y <<<"$x"
    

    You can also do it the way you were initially trying to use:

    y=(${x//$'\n'/ })
    

    This, however, will not function correctly if your string already contains spaces, such as 'line 1\nline 2'. To make it work, you need to restrict the word separator before parsing it:

    IFS=$'\n' y=(${x//$'\n'/ })
    

    ...and then, since you are changing the separator, you don't need to convert the \n to space anymore, so you can simplify it to:

    IFS=$'\n' y=($x)
    

    This approach will function unless $x contains a matching globbing pattern (such as "*") - in which case it will be replaced by the matched file name(s). The read/readarray methods require newer bash versions, but work in all cases.

    0 讨论(0)
提交回复
热议问题