加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何使用bash替换在列表的每个元素的末尾附加换行符

发布时间:2020-12-15 21:54:56 所属栏目:安全 来源:网络整理
导读:我正在寻找一个bash one liner,它在列表的每个元素后附加一个换行符.如果我将脚本称为: ./script arg1 arg2 arg3 我想要输出 arg1arg2arg3 我尝试了以下不同的变化.换行没有添加.添加任何普通的char. # pretty much works except for an extra spacelist=${
我正在寻找一个bash one liner,它在列表的每个元素后附加一个换行符.如果我将脚本称为:

./script arg1 arg2 arg3

我想要输出

arg1
arg2
arg3

我尝试了以下不同的变化.换行没有添加.添加任何普通的char.

# pretty much works except for an extra space
list=${@/%/x}
echo "$list"

# appends 'n'
list=${@/%/n}
echo "$list"

# appends nothing
list=${@/%/$'n'}
echo "$list"

# appends nothing,x078 would append 'x'
list=${@/%/$'x0D'}
echo "$list"

# appends nothing
CR=$'n'
list=${@/%/$CR}
echo "$list"

# same issues with arrays
tmp=($@)
list=${tmp/%/n}
echo "$list"

您建议使用什么修复或替代方案?我显然可以写一个循环或调用tr,但这正是我认为我可以避免使用bash替换.

解决方法

您可以将此功能与“$@”一起使用:

f() { printf "%sn" "$@"; }
f arg1 arg2 arg3
arg1
arg2
arg3

按照男人的打击

@   Expands  to  the positional parameters,starting from one.  When the expansion occurs
     within double quotes,each parameter expands to a separate word.  That is,"$@" is
     equivalent to "$1" "$2" ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读