Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Is it possible to retain trailing newlines when storing the output of a command in a variable?
$ echo "foo " # newline appears after foo foo $ MYVAR="$(echo "foo ")" $ echo "${MYVAR}" # newline is stripped foo $
您可以在流的末尾添加一个哨兵:
$ my_var=$'foo\n\n' $ captured=$( echo -n "$my_var"; echo -n "x" )
然后删除:
$ captured=${captured%x} $ echo "$captured"