<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>シェルスクリプト　文字列　結合 &#8211; スグブログ</title>
	<atom:link href="https://sugublog.org/tag/%e3%82%b7%e3%82%a7%e3%83%ab%e3%82%b9%e3%82%af%e3%83%aa%e3%83%97%e3%83%88%e3%80%80%e6%96%87%e5%ad%97%e5%88%97%e3%80%80%e7%b5%90%e5%90%88/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Thu, 07 Mar 2024 09:18:47 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://sugublog.org/wp-content/uploads/2021/07/icon_20210703-min-150x150.png</url>
	<title>シェルスクリプト　文字列　結合 &#8211; スグブログ</title>
	<link>https://sugublog.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【シェルスクリプト基礎】文字列を結合するには？改行、スペースなども結合</title>
		<link>https://sugublog.org/shell-screipt-base-combine-strings/</link>
					<comments>https://sugublog.org/shell-screipt-base-combine-strings/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Tue, 11 Apr 2023 06:48:16 +0000</pubDate>
				<category><![CDATA[シェルスクリプト]]></category>
		<category><![CDATA[シェルスクリプト　文字列　結合]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5493</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/randam_0915-4-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>シェルスクリプトで文字列を結合したい場合などのやり方をまとめます。 変数と文字列の結合や改行、スペース、空白などを含めた結合、また引数と文字列なども結合していきます。 シェルスクリプトで文字列を結合する シェルスクリプト [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/randam_0915-4-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプトで文字列を結合したい</span></strong>場合などのやり方をまとめます。</p>



<p>変数と文字列の結合や改行、スペース、空白などを含めた結合、また引数と文字列なども結合していきます。</p>


<div class="p-blogParts post_content" data-partsID="5030">
<p>windowsのプロンプトで使用できるbatやpythonやphpを使用して、業務効率化やWebサイトを作成しております。javaやシェルスクリプトなど、様々なプログラミングを行った実績ありの筆者です。</p>
</div>


<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">シェルスクリプトで文字列を結合する</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="576" src="https://sugublog.org/wp-content/uploads/2021/08/programing_0812-2-1024x576.jpg" alt="script" class="wp-image-3963" srcset="https://sugublog.org/wp-content/uploads/2021/08/programing_0812-2-1024x576.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-2-300x169.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-2-768x432.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-2.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">Internet information data flow concept with binary number on screen, close up and shallow dept of field</figcaption></figure>
</div>


<p>シェルスクリプトで文字列を結合する基本は以下の通りです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

str1=string1
str2=string2

echo $str1$str2</code></pre></div>



<p>そのまま<strong><span class="swl-inline-color has-swl-deep-01-color">文字列を並べるだけ</span></strong>で結合可能です。</p>



<p class="is-style-icon_pen">変数に入れた文字列を結合して$で展開した結果をechoコマンドで表示しています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/school_08-3-300x188.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-echo/">【シェルスクリプト基礎】echoコマンドの使い方！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトのechoコマンドの使い方で疑問に思っている事などないでしょうか？ 思い通りに文字列を表示できなかったりしないでしょうか？ シェルスクリプトでecho&#8230;</span>					</div>
				</div>
			</div>
		</div>

<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-3-300x232.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-dollar-mean/">【シェルスクリプト】「$」の意味を詳しく解説！実例あり</a>
						<span class="p-blogCard__excerpt">シェルスクリプトで「$」はよく出てきますが、どういう意味なのかわからないと言った事はないでしょうか？ シェルスクリプトで「$」はよく出てきますが、どういう意味な&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>シェルスクリプトを「bash」コマンドで実行してみます。</p>



<p class="is-style-icon_info">シェルスクリプトを実行するやり方は、以下の記事を参考にして見て下さい。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -external" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">スグブログ</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/script_0809-2.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-execution/" target="_blank" rel="noopener noreferrer">【シェルスクリプト基礎】コマンドを実行する様々な方法をご紹介！ | 【初心者向け】エラー対処法あり！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトを実行する時に困った事はありませんか？ シェルスクリプトのコマンド実行での失敗メッセージ一覧 「実行権限がありません」 「コマンドが見つかりません&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>実行結果は以下の通りです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash combine_strings.sh
string1string2</code></pre></div>



<p>シェルスクリプトで文字列を結合できています。</p>



<h2 class="wp-block-heading">シェルスクリプトで変数に文字列を結合する</h2>



<p>シェルスクリプトで<strong><span class="swl-bg-color has-swl-deep-01-background-color">変数に文字列を結合</span></strong>するやり方は以下のコードを見て下さい。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

str1=string1

echo ${str1}plus</code></pre></div>



<p>変数を{}で囲います。その後に文字列を記入すれば結合出来ます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash combine_strings.sh
string1plus</code></pre></div>



<p>変数と文字列が結合できています。</p>



<p class="is-style-icon_book">逆に変数の文字列から切り出して抽出する方法は以下の記事を参照下さい。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/07/script111-300x175.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shellscript-cutting-and-extracting-variable-strings/">【シェルスクリプト】変数の文字列を切り出して抽出する方法！</a>
						<span class="p-blogCard__excerpt">shellscriptで変数の少し違う文字列を別の変数を作成して使用していると、変数管理が複雑化するので何とかしたい そういった時に変数の文字列を切り出して抽出する事で&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">文字列と文字列を改行して連結する</h2>



<p><span class="swl-bg-color has-swl-deep-01-background-color">改行を含めて連結する</span>場合は以下のコードを見て下さい。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

str1=string1
str2=string2

echo -e &quot;$str1\n$str2&quot;</code></pre></div>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>シェルスクリプトで文字列を改行して結合する手順</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>改行コード「\n」を入れる</li>



<li>エスケープシークエンスを解釈するechoのオプション-eをつける</li>



<li>ダブルクォーテーションで囲む</li>
</ul>
</div></div>



<p>これでシェルスクリプトを実行して見ます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash combine_strings.sh
string1
string2</code></pre></div>



<p>改行を含めて文字列を結合できています。</p>



<h2 class="wp-block-heading">シェルスクリプトでスペースや空白を入れて文字列を結合する</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="576" src="https://sugublog.org/wp-content/uploads/2021/09/randam_0915-5-1024x576.jpg" alt="文字列結合" class="wp-image-4170" srcset="https://sugublog.org/wp-content/uploads/2021/09/randam_0915-5-1024x576.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/09/randam_0915-5-300x169.jpg 300w, https://sugublog.org/wp-content/uploads/2021/09/randam_0915-5-768x432.jpg 768w, https://sugublog.org/wp-content/uploads/2021/09/randam_0915-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><span class="swl-bg-color has-swl-deep-01-background-color">空白やスペースを入れて文字列を結合する</span>には以下のコードを見て下さい。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

str1=string1
str2=string2

echo $str1 $str2 #空白１つ
echo $str1  $str2 #空白２つだけど空白1つ分
echo &quot;$str1  $str2&quot; #空白２つ</code></pre></div>



<p class="is-style-icon_pen">シェルスクリプトでの「#」はコメントアウトを意味します。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/school_08-2-300x200.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/syell-script_comment_out/">【シェルスクリプト基礎】コメントアウトのやり方！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトでコメントを入れたいけど入れ方がわからない。 また複数行コメントアウトできればやりたいといった要望はあると思います。 そこで今回はシェルスクリ&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>空白を１つ入れる分にはそのまま文字列を記述すれば可能です。</p>



<p><span class="swl-marker mark_yellow">空白を<strong>複数</strong>いれても実際は１つになります。</span></p>



<p class="is-style-big_icon_check">ダブルクォーテーションで囲む事で複数の空白も表現可能です。</p>



<p>では上のシェルスクリプトを実行して見ます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash combine_strings.sh
string1 string2
string1 string2
string1  string2</code></pre></div>



<p>想定通りの結果となりました。</p>



<h2 class="wp-block-heading">シェルスクリプトで文字列と引数を結合する</h2>



<p><span class="swl-bg-color has-swl-deep-01-background-color">文字列と引数を結合する</span>場合は以下のコードを見て下さい。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

str1=string1
str2=string2

echo $str1$1</code></pre></div>



<p><strong><span class="swl-marker mark_orange">引数の1番目を参照したい場合は「$1」で可能</span></strong>です。</p>



<p>今まで通り「$1」を結合すれば可能になります。</p>



<p>では引数に「plus」を入れてシェルスクリプトを実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash combine_strings.sh plus
string1plus</code></pre></div>



<p>「string1」と「plus」の文字が結合できています。</p>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>


<div class="p-blogParts post_content" data-partsID="6113">
<div class="wp-block-columns">
<div class="wp-block-column">
<a href="https://px.a8.net/svt/ejp?a8mat=3TNJ87+8UIXF6+5EPG+65U41" rel="nofollow">
<img decoding="async" border="0" width="250" height="250" alt="" src="https://www29.a8.net/svt/bgt?aid=231205399535&#038;wid=001&#038;eno=01&#038;mid=s00000025234001035000&#038;mc=1"></a>
<img decoding="async" border="0" width="1" height="1" src="https://www18.a8.net/0.gif?a8mat=3TNJ87+8UIXF6+5EPG+65U41" alt="">
</div>



<div class="wp-block-column">
<a href="https://px.a8.net/svt/ejp?a8mat=3HP27C+14HHWY+3XAE+69P01" rel="nofollow">
<img decoding="async" border="0" width="300" height="250" alt="" src="https://www27.a8.net/svt/bgt?aid=211121256068&#038;wid=001&#038;eno=01&#038;mid=s00000018311001053000&#038;mc=1"></a>
<img decoding="async" border="0" width="1" height="1" src="https://www11.a8.net/0.gif?a8mat=3HP27C+14HHWY+3XAE+69P01" alt="">
</div>
</div>
</div>


<h2 class="wp-block-heading">シェルスクリプトで文字列を結合するのまとめ</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="522" src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-1024x522.jpg" alt="文字列結合" class="wp-image-4417" srcset="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-1024x522.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-300x153.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-768x392.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>シェルスクリプトで文字列を結合する基本は以下の通りです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

str1=string1
str2=string2

echo $str1$str2</code></pre></div>



<p>そのまま文字列を並べるだけで結合可能です。</p>



<p>その他にも様々な結合を試しました。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>その他の文字列結合</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>変数と文字列の結合</li>



<li>改行を含めた文字列の結合</li>



<li>スペースや空白を入れた文字列の結合</li>



<li>引数と文字列の結合</li>
</ul>
</div></div>



<p>そのシェルスクリプトのコードを以下にまとめています。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

str1=string1
str2=string2
#文字列の結合基礎
echo $str1$str2

#変数と文字列の結合
echo ${str1}plus

#改行を含めた文字列の結合
echo -e &quot;$str1\n$str2&quot;

#空白やスペースを含めた文字列の結合
echo $str1 $str2 #空白１つ
echo $str1  $str2 #空白２つだけど空白1つ分
echo &quot;$str1  $str2&quot; #空白２つ

#引数と文字列の結合
echo $str1$1</code></pre></div>



<p>使用できそうな文字列の結合があれば、利用してみて下さい。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/shell-screipt-base-combine-strings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
