sed コマンドの変数にはエスケープシーケンスを使用できません。

sed コマンドの変数にはエスケープシーケンスを使用できません。

sedコマンド出力で列を強調表示するために使用し、psカラーコードをハードコーディングすると正しく機能します。ただし、色を動的に表現する必要があるため、変数に移動してsed命令でその変数を参照しようとしましたが、まったく動作しないようです。

# Works
ps -eo pid,ppid,time,user,tty,%cpu,%mem,vsize,command --sort -%cpu | head  | sed -e $'s/ *[^ ]* /\033[1;33m&\033[0m/6'

ただし、\033[1;33mおよびを\033[0m変数に移動してコマンドで参照すると(さまざまな方法)、まったく機能しません。

# None of the following work
fg_normal="\033[0m"
fg_yellow="\033[1;33m"

ps -eo pid,ppid,time,user,tty,%cpu,%mem,vsize,command --sort -%cpu | head  | sed -e $'s/ *[^ ]* /'$fg_yellow'&'$fg_normal'/6'
ps -eo pid,ppid,time,user,tty,%cpu,%mem,vsize,command --sort -%cpu | head  | sed -e $'s/ *[^ ]* /'"$fg_yellow"'&'"$fg_normal"'/6'
ps -eo pid,ppid,time,user,tty,%cpu,%mem,vsize,command --sort -%cpu | head  | sed -e $"s/ *[^ ]* /$fg_yellow&$fg_normal/6"

どんなアイデアがありますか?

ベストアンサー1

あなたが使用できる:

fg_normal=$(echo -e "\033[0m")
fg_yellow=$(echo -e "\033[1;33m")

またはtputを使用してください:

fg_normal=$(tput sgr0)
fg_yellow=$(tput setaf 3)

おすすめ記事