不起作用
echo ${REV%%\n*}
有效吗
echo ${REV%%
*}
阅读http://tldp.org/LDP/abs/html/parameter-substitution.html之后,我仍然无法弄清楚如何使其工作。
答案 0 :(得分:12)
${REV%%$'\n*'}
似乎有效。请参阅the quoting section of the bash documentation。
答案 1 :(得分:2)
如果您打算尝试将其放在一行并且您愿意在bash“之外”,则可以使用:
echo "$(echo "${REV}" | head -1l)"
但是,假设您的bash
版本已经足够,您可以尝试:
pax> export REV="abc
...> def"
pax> echo "${REV}"
abc
def
pax> echo "${REV%%$'\n'*}"
abc
您需要$'\n'
的原因是因为bash
word
的定义与您期望的相比有些限制。 bash
联机帮助页有这样的说法:
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 :(得分:0)
更便于携带(例如POSIX):
var="yo
yo"
newline="
"
echo "${var#*"$newline"}"