linux - 从 bash 中的变量打印倒数第二行

标签 linux bash macos printf

VAR="1\n2\n3"

我正在尝试打印倒数第二行。 bash 中的一行!

到目前为止我已经得到了:printf -- "$VAR"|头-2

然而它打印出太多。

我可以用文件做到这一点没问题:tail -2 ~/file |头-1

最佳答案

您几乎一个人完成了这项任务。尝试

VAR="1\n2\n3"; printf -- "$VAR"|tail -2|head -1

关于linux - 从 bash 中的变量打印倒数第二行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52481112/

相关文章:

regex - grep/egrep 星号运算符不匹配所有出现的情况

Bash 无意中分割命令

c++ - webassembly 和 get_nprocs()

linux - fork() 机制的一部分

linux - 无法上传我的 Recipe

python - Jupyter notebook 命令在 Mac 上不起作用

objective-c - 第二个最前面的应用程序?

c++ - 运行进程的 CLI 命令

linux - 没有值的 Shell 脚本参数

Git总是提示 "Are you sure you want to continue connecting (yes/no/[fingerprint])?"