Get the App
SLTechnology News&Howtos  ›  Development  › 

How to concatenate and merge Shell strings

Shulou Source: shulou.com Published: 2022-06-03 05:30:30 10月09日 Update

This article will explain in detail how to concatenate and merge Shell strings. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

In scripting languages, string concatenation (also known as string concatenation or string merging) is often very simple, such as:

In PHP, use. You can concatenate two strings; in JavaScript, you can merge two strings into one by using +.

However, you don't need to use any operators in Shell, and you can concatenate two strings side by side, which is very simple and rough. Take a look at the following example:

#! / bin/bash

Name= "Shell"

Url= "http://c.biancheng.net/shell/"

There can be no spaces in the str1=$name$url#

Str2= "$name$url" # if surrounded by double quotes, there can be spaces in the middle

Another string can appear in the middle of str3=$name ":" $url#.

Str4= "$name:$url" # you can write it this way

Str5= "${name} Script:$ {url} index.html" # this time you need to put curly braces on the variable name

Echo$str1

Echo$str2

Echo$str3

Echo$str4

Echo$str5

Running result:

Shell http://c.biancheng.net/shell/

Shell http://c.biancheng.net/shell/

Shell: http://c.biancheng.net/shell/

Shell: http://c.biancheng.net/shell/

ShellScript: http://c.biancheng.net/shell/index.html

For line 7, a space cannot appear between $name and $url because when a string is not surrounded by any kind of quotation marks, the string is considered to be over when it encounters a space, and the contents after the space are parsed as other variables or commands.

For line 10, add {} to help the interpreter identify the boundaries of variables.

Shell is very similar to Python in how strings are concatenated. Python supports both concatenating strings with + and putting two strings together.

On how to carry out Shell string concatenation and merge to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Characters strings two content variables spaces code quotation marks articles more knowledge articles help support good rough reasons between examples commands Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia MySQL Apple Redmi