第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在sh中的字符串中添加換行符?

如何在sh中的字符串中添加換行符?

慕尼黑5688855 2019-08-30 14:08:51
這個(gè)STR="Hello\nWorld"echo $STR產(chǎn)生輸出Hello\nWorld代替HelloWorld如何在字符串中添加換行符?注意:這個(gè)問題不是關(guān)于回聲。 我知道echo -e,但我正在尋找一種解決方案,允許將一個(gè)字符串(包括換行符)作為參數(shù)傳遞給其他命令,這些命令沒有類似的選項(xiàng)來解釋\n為換行符。
查看完整描述

3 回答

?
達(dá)令說

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊

解決方案是使用$'string',例如:


$ STR=$'Hello\nWorld'

$ echo "$STR"

Hello

World

以下是Bash手冊(cè)頁的摘錄:


   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)

          \cx    a control-x character


   The expanded result is single-quoted, as if the  dollar  sign  had  not

   been present.


   A double-quoted string preceded by a dollar sign ($"string") will cause

   the string to be translated according to the current  locale.   If  the

   current  locale  is  C  or  POSIX,  the dollar sign is ignored.  If the

   string is translated and replaced, the replacement is double-quoted.


查看完整回答
反對(duì) 回復(fù) 2019-08-30
?
德瑪西亞99

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊

Echo是九十年代,充滿了危險(xiǎn),它的使用應(yīng)該導(dǎo)致核心轉(zhuǎn)儲(chǔ)不低于4GB。說真的,echo的問題是Unix標(biāo)準(zhǔn)化過程最終發(fā)明printf實(shí)用程序的原因,消除了所有問題。


所以要在字符串中獲取換行符:


FOO="hello

world"

BAR=$(printf "hello\nworld\n") # Alternative; note: final newline is deleted

printf '<%s>\n' "$FOO"

printf '<%s>\n' "$BAR"

那里!沒有SYSV和BSD回聲瘋狂,一切都得到了整齊的打印和完全便攜式支持C轉(zhuǎn)義序列。大家請(qǐng)printf現(xiàn)在使用,永遠(yuǎn)不要回頭。


查看完整回答
反對(duì) 回復(fù) 2019-08-30
?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

我根據(jù)其他答案做了什么


NEWLINE=$'\n'

my_var="__between eggs and bacon__"

echo "spam${NEWLINE}eggs${my_var}bacon${NEWLINE}knight"


# which outputs:

spam

eggs__between eggs and bacon__bacon

knight


查看完整回答
反對(duì) 回復(fù) 2019-08-30
  • 3 回答
  • 0 關(guān)注
  • 1095 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)