New line in bash parameter substitution $ {REV %% \ n *}

does not work

echo ${REV%%\n*} 

works

 echo ${REV%% *} 

After reading the deflection http://tldp.org/LDP/abs/html/parameter-substitution.html I still canโ€™t figure out how to get the job done \ n.

+4
source share
2 answers

${REV%%$'\n*'} seems to work. See the citation section of bash documentation .

+11
source

If you intend to try and get it on the same line, and you are ready to go "outside" from bash, you can use:

 echo "$(echo "${REV}" | head -1l)" 

But, if your bash version is quite recent, you can try:

 pax> export REV="abc ...> def" pax> echo "${REV}" abc def pax> echo "${REV%%$'\n'*}" abc 

The reason you need $'\n' is because the definition of bash word somewhat restrictive compared to what you expect. The bash man page has the following:


  Words of the form $ 'string' are treated specially.  The word expands
 to string, with backslash-escaped characters replaced as specified by
 the ANSI C standard.  Backslash escape sequences, if present, are decoded
 as follows:
    \ a alert (bell)
    \ b backspace
    \ e
    \ E an escape character
    \ f form feed
    \ n new line
    \ r carriage return
    \ t horizontal tab
    \ v vertical tab
    \\ backslash
    \ 'single quote
    \ "double quote
    \ nnn the eight-bit character whose value is the octal value
           nnn (one to three digits)
    \ xHH the eight-bit character whose value is the hexadecimal
           value HH (one or two hex digits)
    \ uHHHH the Unicode (ISO / IEC 10646) character whose value is
           the hexadecimal value HHHH (one to four hex digits)
    \ UHHHHHHHH
           the Unicode (ISO / IEC 10646) character whose value is
           the hexadecimal value HHHHHHHH (one to eight hex digits)
    \ cx a control-x character
+2
source

All Articles