在不使用任何其他命令的情况下,在同一变量集行中组合多个bash参数替换 [英] Combining multiple bash parameter substitutions within same variable set line without using any other commands

查看:113
本文介绍了在不使用任何其他命令的情况下,在同一变量集行中组合多个bash参数替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结合的例子:

sVar=$(whoami)
sVar=${sVar^}
sVar=${sVar::1}

输出:

  • 用户名的大写第一个字符

要求:

  • 单线
  • 除参数$(whoami)以上的初始命令替换外,其余所有处理均使用参数替换

我意识到这可以通过tr,sed,awk,printf,cut等来完成;但这不是问题的重点.

I realize this can be done with tr, sed, awk, printf, cut, etc.; but that is not the point of the question.

感谢您的帮助!

这不是真实的代码,也不表示我想要实际执行的操作.在连接多个命令时,我经常会默认(或尝试)仅使用一个命令.

This isn't the real code or anything indicative of what I am wanting to actually do. I will often default (or try to) to using just one command over concatenating multiple commands.

我已经看到其他帖子指出在括号内进行连接是不可能的,但是我知道一切皆有可能.

I've seen other posts state that concatenating within the braces are not possible, but I know that everything is possible.

请不要:

  • 将其他帖子重复一遍,说这是不可能的

推荐答案

一个衬里:

sVar=$(whoami) sVar=${sVar^} sVar=${sVar::1}

它可能不是您想要的,但是它肯定是一个命令,并使用参数替换.一个命令可以包含多个变量分配,在这种情况下,它们是从左到右执行的.

It may not be what you were looking for, but it is certainly a single command and uses parameter substitutions. A single command can consist of multiple variable assignments, in which case they are performed left to right.

参数替换的主题是变量名称,间接寻址(后跟变量名称)或下标表达式(变量名-不是间接寻址-后跟 [] 中的一个下标.可以肯定的是,这有点受限制,但这是bash.(Posix shell的限制更大;它既没有间接寻址也没有数组,并且扩展类型较少.)

The subject of a parameter substitution is a variable name, an indirection (a ! followed by a variable name), or a subscript expression (a variable name -- not an indirection -- followed by a subscript within [ and ]. That's a bit limited, to be sure, but that's bash. (Posix shell is even more restrictive; it has no indirection nor arrays, and fewer types of expansion.)

XCU上的Posix措词,并增加了重点:

Posix wording from XCU, emphasis added:

当需要执行给定的简单命令时…以下扩展,分配和重定向均应从命令文本的开头到结尾全部执行

  1. 在分配值之前,应先扩展波浪号扩展,参数扩展,命令替换,算术扩展和引号删除的每个变量分配.

这篇关于在不使用任何其他命令的情况下,在同一变量集行中组合多个bash参数替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆