Remove line breaks in Bourne Shell from variable

后端 未结 4 1786
猫巷女王i
猫巷女王i 2021-02-07 07:45

In bourne shell I have the following:

VALUES=`some command that returns multiple line values`

echo $VALUES

Looks like:

\"ONE\"         


        
相关标签:
4条回答
  • 2021-02-07 07:49

    Another option is using xargs (which keeps a final newline though - instead of a possible trailing space using tr):

    echo $VALUES | xargs
    printf '%s\n' 1 2 3 4 5 | xargs
    

    @yozloy: how to pass escaped string using <<<

    tr -d '\n' <<< "`printf '%b' 'a line with line feed \n'`"
    
    0 讨论(0)
  • 2021-02-07 07:54

    echo $VALUES | tr '\n' ' '

    0 讨论(0)
  • 2021-02-07 07:58

    The accepted solution didn't work for me (on OS X Yosemite). This is what I used:

    echo -n $VALUES

    0 讨论(0)
  • 2021-02-07 08:03

    Another method, if you want to not just print out your code but assign it to a variable, and not have a spurious space at the end:

    $ var=$(tail -1 /etc/passwd; tail -1 /etc/passwd)
    $ echo "$var"
    apache:x:48:48:Apache:/var/www:/sbin/nologin
    apache:x:48:48:Apache:/var/www:/sbin/nologin
    $ var=$(echo $var)
    $ echo "$var"     
    apache:x:48:48:Apache:/var/www:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin
    
    0 讨论(0)
提交回复
热议问题