如何在bash脚本中连接变量和字符串

编程入门 行业动态 更新时间:2024-10-23 15:20:26
本文介绍了如何在bash脚本中连接变量和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在bash脚本中连接变量和字符串?

How to concat variable and string in bash script ?

val1 = Variable1 + "any string "

例如:

val1 = $i + "-i-*"

其中i = 3月24日

where i = 24thMarch

我想要回声val1:

24thMarch-i-*

什么是合适的解决方案?

What is proper proper to get the solution ?

推荐答案

默认情况下,字符串在外壳中是串联的.

Strings are concatenated by default in the shell.

value="$variable"text"$other_variable"

通常认为将变量扩展用双引号引起来是一种很好的做法.

It's generally considered good practice to wrap variable expansions in double quotes.

您也可以执行以下操作:

You can also do this:

value="${variable}text${other_variable}"

大括号在处理变量名和字符串的混合体时很有用.

The curly braces are useful when dealing with a mixture of variable names and strings.

请注意,作业中=周围不应有空格.

Note that there should be no spaces around the = in an assignment.

更多推荐

如何在bash脚本中连接变量和字符串

本文发布于:2023-10-27 03:24:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   变量   脚本   如何在   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!