首页 > 系统相关 >How to add a string that contains whitespace to array in shell script All In One

How to add a string that contains whitespace to array in shell script All In One

时间:2023-09-27 19:23:26浏览次数:36  
标签:arr shell string script strs str2 str1 item whitespace

How to add a string that contains whitespace to array in shell script All In One

I have tried some ways to add a string which contain whitespace to array in shell script, but failed.

image

string variables concatenate

#!/usr/bin/env bash

str1="hello"
str2="world"
# ✅
strs="$str1 $str2"
# or ✅
# strs+="$str1 "
# strs+="$str2"
# or ✅
# strs="$str1 ""$str2"

echo $strs
# hello world

error

But that not work when I add a string which contain a whitespace to array.
It splits the string into two items.

#!/usr/bin/env bash

str1="hello"
str2="world"
# ❌
# strs="$str1\ $str2"
# strs="$str1'\ '$str2"
strs="$str1 $str2"

arr=()
arr+=("$strs")

for item in ${arr[@]}; do
  echo "item = $item"
done
# item = hello
# item = world

tried

After I remove the whitespace, use other symbol it works, but not the expected result.

#!/usr/bin/env bash

str1="hello"
str2="world"
# ❓
# strs="$str1"-"$str2"
strs="$str1"_"$str2"

arr=()
arr+=("$strs")

for item in ${arr[@]}; do
  echo "item = $item"
done
# item = hello_world

So, what's wrong with that, and how to fix it.

solution ✅

I want to get only one item with whitespace.

#!/usr/bin/env bash

str1="hello"
str2="world"
# ❓
strs="string with whitespace"

arr=()
arr+=("$strs")

# ✅ 双引号
for item in "${arr[@]};" do
  echo "item = $item"
done
# item = hello world

image

demos

(

标签:arr,shell,string,script,strs,str2,str1,item,whitespace
From: https://www.cnblogs.com/xgqfrms/p/17734114.html

相关文章

  • linux-Shell将命令行终端输出结果写入保存到文件中
    (一)将输出与错误写到同一个文件(1)方法1#!bin/bashjava-jarhbase-example.jar2>&1|teehbase_log.txt说明:0,1,2:在linux分别表示标准输入、标准输出和标准错误信息输出。tee默认为写入覆盖,-a参数表示追加内容。#!bin/bashjava-jarhbase-example.jar2>&1|tee-ahbase_......
  • 无涯教程-JavaScript - TREND函数
    描述TREND函数沿线性趋势返回值。(使用最小二乘法)将一条直线拟合到阵列known_y和known_x。返回指定的new_x数组沿该行的y值。语法TREND(known_y's,[known_x's],[new_x's],[const])争论Argument描述Required/OptionalKnown_y's在关系y=mx+b中,您已经知道的y值......
  • 无涯教程-JavaScript - TREND函数
    描述TREND函数沿线性趋势返回值。(使用最小二乘法)将一条直线拟合到阵列known_y和known_x。返回指定的new_x数组沿该行的y值。语法TREND(known_y's,[known_x's],[new_x's],[const])争论Argument描述Required/OptionalKnown_y's在关系y=mx+b中,您已经知道的y......
  • [译]JavaScript规范
    译自:https://github.com/airbnb/javascript类型原始值:相当于传值1.string2.number3.boolean4.null5.undefined6.varfoo=1,7.bar=foo;8.9.bar=9;10.11.console.log(foo,bar);//=>1,9复杂类型:相当于传引用1.obje......
  • JavaScript——“==”和“===”区别(双等于号和三等于号区别)
    1.==:双等于号称为等值符。当等号两边为类型相同的值时,则直接比较值是否相同;当类型不同时,会先进行类型转换,转换为相同的类型后再进行比较。类型转化规则:1)当等号两边为boolean,string,number三者中任意两者进行对比时,会优先转换为number进行比较2)当等号两......
  • JavaScript下载base64位文件
    1/**2*下载文件3**/4functiondownloadExcel(base64Data){5varmyBlob=this.base64toBlob(base64Data);6varmyUrl=URL.createObjectURL(myBlob);7varlink=document.createElement("a");8......
  • nginx访问报错“maximum number of descriptors supported by select() is 1024 while
    1、问题背景 项目:一个人力的系统,主要用于考勤打卡环境:windowsservernginx版本:1.22 问题说明:当早上访问人数增加的时候,就会出现nginx的异常nginx的后台报错日志:maximumnumberofdescriptorssupportedbyselect()is1024whileconnectingtoupstream  ......
  • 使用CSS、HTML、JavaScript实现一个简单的身份验证页
      这是我在博客园的第一篇博客,也是我人生中的第一篇博客。希望它能够记录我的成长,帮助更多的人。  最近在写我们社团的社团网站,有一个页面不太希望普通访客能访问到,所以想做一个“统一身份验证验证”,但是又苦于社团网站搭建是纯静态站,没法做数据库,只能妥协,将账号密码字符串......
  • Java String类的 equals、==和intern()
    Java实例的生成我们都知道,java中new一个类的实例是在JVM的堆中完成的,如下图所示:在这里我们以String类为例讲解一些更为细节的东西!String生成实例的代码如下:String str=new String("hello");对于通过new产生一个字符串(假设为” hello”)时,会先去上图的常量池中查找是否已经有了......
  • 详解如何使用VS code搭建JavaScript环境(适合小白)
    对于从事自动化测试的同学来说,有很多自动化测试项目是需要使用JavaScript脚本语言进行coding的,包括selenium、playwright、Puppeteer,那么选择哪种IDE合适呢?在这里我推荐visualstudiocode,即vscode!注意:本文介绍的是JavaScript在后端运行和调试的方法,并未涉及前端(浏览器)相关开发......