如何在bash脚本中连接变量和字符串 [英] How to concat variable and string in bash script

查看:152
本文介绍了如何在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脚本中连接变量和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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