test1="one two three four five"
echo $test1 | cut -d $'\t' -f2
Run Code Online (Sandbox Code Playgroud)
我有一个以 分隔的字符串TAB,我想通过命令获取第二个单词cut。
我发现了问题How to split a string in bash delimited by tab。但该解决方案不与 一起使用cut。
发生这种情况是因为您需要$test1在以下情况下引用echo:
echo "$test1" | cut -d$'\t' -f2
Run Code Online (Sandbox Code Playgroud)
否则,格式就会消失,制表符会转换为空格:
$ s="hello bye ciao"
$ echo "$s" <--- quoting
hello bye ciao
$ echo $s <--- without quotes
hello bye ciao
Run Code Online (Sandbox Code Playgroud)