2012-06-28 47 views
2

鑑於變量如下:突出顯示回聲

​​

我如何呼應$text與被着色在bash $color$adjective值,但不能改變的變量?

實施例:

$ echo -e --highlight-variables $text 
The quick \e[00;31mbrown\e[00m fox jumps over the \e[00;31mlazy\e[00m dog 

(除了與實際顏色)

但隨後:

$ echo -e $text 
The quick brown fox jumps over the lazy dog 

將沒有顏色變化。

我希望能夠輸出與less文本,以便用戶可以在控制檯中查看它,但然後將變量寫入文件,但文件中不應該有顏色信息。我執行的實際代碼看起來是這樣的:

echo -e $text | less 
echo "Is this acceptable?" 
read accept 
if [ "$accept" == "y" ] 
    then 
    echo -e $text > output.txt 
fi 

如果我換顏色的變量,逃脫的文本被擺在文件中。

+0

你不能這樣做。沒有辦法知道擴展發生在哪裏。您必須使用一個數組並將拼寫代碼(應該從tput生成)和文本拼接在一起。 – ormaaj

回答

1

您只需要在打印之前刪除ansi序列。

nocolor() 
{ 
perl -pe "s/\e\[\d+(?>(;\d+)*)m//g;" 
} 

echo -e $text | less 
echo "Is this acceptable?" 
read accept 
if [ "$accept" == "y" ] 
    then 
    echo -e $text | nocolor > output.txt 
fi