# Concatenate two values using KSH on HP-UX

**URL:** <https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071>\
**Category:** HP-UX\
**Created:** [April 1, 2022, 10:25pm UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071 "2022-04-01T22:25:02Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 1, 2022, 10:25pm UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/1 "2022-04-01T22:25:02Z")

</div>

I need to Concatenate the value of DIR in a while read loop with the value of another field called $counter. Please advise. Thanks.

```nohighlight
while read DIR
do
     ?
```

---

<div class="post-metadata">

**Author:** ![drysdalk](https://community.unix.com/letter_avatar/drysdalk/32/6_22734026db12803ebb3e059622ac3534.png) [@drysdalk](https://community.unix.com/u/drysdalk)\
**Post date:** [April 1, 2022, 10:38pm UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/2 "2022-04-01T22:38:56Z")

</div>

Hello,

Essentially, you handle variables within a `while` loop more or less the same way you do anywhere else in a shell script. The fact that they are within a `while` loop doesn't really change anything, in terms of how you can use variables or the types of things you can do with them.

A couple of quick examples of ways of combining variables together:

```bash
#!/bin/bash
fruit=apple
number=1

echo "$fruit""$number"
echo $fruit$number

fruitplusnumber="$fruit$number"

echo "$fruitplusnumber"

```

And when we run this, we get the exact same output all three times:

```shell
$ ./script.sh 
apple1
apple1
apple1
$ 

```

Hope this helps !

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [April 2, 2022, 12:52am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/3 "2022-04-02T00:52:29Z")

</div>

@rtb ,

please supply actual examples of expected input and desired output, @drysdalk has very generously provided a decent example, but ultimately this is for you to solve

NB: we are not here to your solutions but to assist you in coming to those, we may be able to provide alternative once a solution has been presented .... but ultimately if we write your code .... then you've gained very little if anything at all.

---

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 6, 2022, 1:12am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/4 "2022-04-06T01:12:32Z")

</div>

Well you know, I am not really asking anyone to write code for me, but if it seems like it, it’s probably because I already searched high and low on the internet for an example, perhaps not realizing I am staring at the answer. Now, I may have missed the answer in my searching the internet, even after browsing through topics on this site, but I did exhaust all attempts to find a solution before posting a topic on this site. Another reason is as I already stated in previous posts, that I am really new to scripting. So, your patience and the patience of others would be appreciated.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 3:39am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/5 "2022-04-06T03:39:29Z")

</div>

> [@rtb](#):
>
> Well you know, I am not really asking anyone to write code for me, but if it seems like it, it’s probably because I already searched high and low on the internet for an example, perhaps not realizing I am staring at the answer

Well, I just read your other post which I commented on, and it is hard to believe what you said above.

If you want to write a `while` statement in `bash`, then you simply google for a reference,

 ![Screen Shot 2022-04-06 at 10.15.43 AM](https://community.unix.com/uploads/default/original/3X/3/f/3f0cb2ea0999c7ace48e926101c505980c70162e.png)

Then, you quickly find a tutorial you like and you follow it.

For example:

[https://www.cyberciti.biz/faq/bash-while-loop/](https://www.cyberciti.biz/faq/bash-while-loop/)

If you want to concatenate strings in `bash`, you do the same thing.

 ![Screen Shot 2022-04-06 at 10.17.47 AM](https://community.unix.com/uploads/default/original/3X/8/5/85020af6c1c911dd0554a4fdfd53d3fc42d31437.png)

This is how we all write code. If we do not know the correct syntax, we just go find a reference and get the info we need.

You may not realize how lucky we are today. In the old days when we programmed, before the Internet was full of reference sites, we actually had to buy a book to program in a language. Now, you just google for a reference or use a code editor with syntax checking like Visual Studio Code, where you can type "while" and the reference pops up in VSC so no need to even google.

When I learned to code, I spend thousands of dollars on reference books. Now, those days are long, long behind us. We have IDEs, code editors, and google. There is no need to go to the library and find a reference on `C++` because we do not want to spend $60 on another book. It's all free on-line.

What is so hard about finding a reference on how to concatenate in bash?

I simple do not understand @rtb what you are doing? If you have no idea how to code, that's fine, but the first thing you should decide on is what shell or programming language you want to code in. Then, you look at a reference in that language you like and then use the syntax / commands of that language.

it's really simple, believe me.

Are you using bash? What are you trying to do?

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 3:50am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/6 "2022-04-06T03:50:09Z")

</div>

> [@rtb](#):
>
> Another reason is as I already stated in previous posts, that I am really new to scripting. So, your patience and the patience of others would be appreciated.

Yes, but you need to at least decide what language you are going to write your script in.

`bash` is a good choice, so let's go with bash.

Now, that we have decided on `bash`, you should just google, as I have mentioned:

```plaintext
concatenate two strings in bash

```

You will find a million references. You do not need our team to spoon feed you what google yields in milliseconds.

Then, you should, in good faith write code in `bash` to do what you want, based on using a reference. We are not a replacement for using a programming language reference. They are everywhere and free online.

Then after you write code, in good faith, using one or more references, you can post here if you have questions.

Do you understand @rtb?

We are not a substitution for a `bash` reference guide. That's not the best use of our time and expertise. You are very lucky [drysdalk](https://community.unix.com/u/drysdalk) is so generous and one of the nicest people on the planet :). On the other hand, [munkeHoller](https://community.unix.com/u/munkeHoller) is correct as well. You must write your own code (by selecting a language such as `bash` and reading the reference for the commands you think you need) and try writing code, in good faith.

Here ya' go:

### Bash Reference Guide

> **[Bash Reference Manual](https://www.gnu.org/software/bash/manual/bash.html)**
>
> Bash Reference Manual

---

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 6, 2022, 4:11am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/7 "2022-04-06T04:11:50Z")

</div>

The operating system is HP-UNIX. The scripting is not BASH, but KSH. I’ll try to be more precise when looking for something on the internet.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 4:17am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/8 "2022-04-06T04:17:04Z")

</div>

I learned and programmed KSH on HP-UX extensive nearly 35 years ago.

Do you have a `KSH` reference guide @rtb ?

If so, which one(s) are you using?

I used to script exclusively in KSH, but have not played with that shell in a very long time. I use BASH for sys admin tasks, generally speaking.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 4:21am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/9 "2022-04-06T04:21:04Z")

</div>

> [@rtb](#):
>
> The operating system is HP-UNIX. The scripting is not BASH, but KSH.

Since you are not experienced at this, you might be better off scripting in BASH on HP-UX, but I guess you will need to install it first.

See, for example, this old reference. You can surely find newer install guides for BASH on HP-UX.

> **[Install BASH Shell in HP-UX](https://itsiti.com/install-bash-shell-in-hp-ux/)**
>
> 1. Go to below URL page to download the BASH shell and the run time dependencies. Beware to download based on your operating system and architecture

---

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 6, 2022, 4:38am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/10 "2022-04-06T04:38:06Z")

</div>

No, I do not have a ksh reference guide.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 4:41am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/11 "2022-04-06T04:41:55Z")

</div>

Honestly, because people have "moved away" from KSH over the years, it's hard to find good KSH refs on the net.

If I were you, I would serious consider installing BASH and learning BASH.

There are very good BASH reference guides and tutorials on the net.

If you "insist" on KSH, the here is a reference guide for you:

> **[ksh - HP-UX](https://nixdoc.net/man-pages/HP-UX/man1/ksh.1.html)**
>
> HP-UX Manual Page for: ksh (1) -- shell, the standard/restricted command programming language

---

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 6, 2022, 4:42am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/12 "2022-04-06T04:42:51Z")

</div>

Even if there is a BASH shell that could be downloaded, the UNIX administrators where I work would not allow me to do that.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 4:45am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/13 "2022-04-06T04:45:29Z")

</div>

Yes, this is common on many HP-UX production applications.

So, check out this reference or find another one:

> **[ksh - HP-UX](https://nixdoc.net/man-pages/HP-UX/man1/ksh.1.html)**
>
> HP-UX Manual Page for: ksh (1) -- shell, the standard/restricted command programming language

You must learn the basics of KSH syntax as it is not really our role to teach you what you can easily read on a KSH reference page.

We are here to help you after you write your own KSH script where you have actually tried to use basic KSH command and syntax from a KSH reference.

Why?

Well, many of us will google the same reference for KSH to respond to your questions, because we generally do not memorize the syntax of all programming language we use. When I was in engineering school, almost all tests were "open book" because we were expected to use references to solve hard problems, not memorize language specific syntax and commands. We did not go to the university to learn to memorize syntax and solve small problems. We learned how to reference others work and solve complex problems.

So, you @rtb should do the same as we do. Review the KSH reference guides when writing KSH code. Write your own KSH code "in good faith" and post back here valid KSH problems when you are writing KSH code based on using one or more KSH reference guides.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 4:58am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/14 "2022-04-06T04:58:49Z")

</div>

> [@rtb](#):
>
> Even if there is a BASH shell that could be downloaded, the UNIX administrators where I work would not allow me to do that.

Let me also add that I am sure you @rtb have seen the Matrix (the first one).

In that movie, Neo did not go to the net and ask others to show him "kung fu". Someone plugged "kung fu" into his brain. Since we don't have a "KSH chip" to plug into our brains like in the Matrix, we need to read the basic reference material for KSH as we use it.

I am sure you can understand this point, @rtb .

_From my perspective, I woke up this AM to see someone who was asking about scripting (in two posts at the same time) who did not even identify the shell or the OS they were using. I read team members guessing and assuming what you were doing. All you had to do, @rtb was to tell everyone you were on HP-UX and were using KSH and a description of your task. This would not have wasted your time or our time._

Personally, I do not understand why anyone would post in a forum, asking experts questions, and not include the OS and programming language. You cannot order parts for "a car". You must specific the make "BMW", The model "Z4 E89" and the year (2012). The same is true for computers. You must specific the basic "make" and "model" and "version", or at least the "make" (HP-UX) and the "model" (KSH). You actually should specify the KSH version 😉

```plaintext
ksh --version

```

The version of HP-UX would also be useful 🙂

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [April 6, 2022, 7:17am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/15 "2022-04-06T07:17:14Z")

</div>

The shell does not have a string-concatenation operator. You simply chain the two strings.  
As was shown in post 2.

Use (double-)quotes around a `$var` in commands, to avoid an unwanted expansion.

```nohighlight
var1="word"
var2="word2"
echo "$var1""$var2"
echo "$var1$var2"

```

Run this on the command line!  
`echo` is a command.

In the following case you must indicate where the variable name ends:

```nohighlight
var1=word
echo "$var1xxx"
echo "$var1"xxx
echo "$var1""xxx"
echo "${var1}xxx"
echo "$var1"'xxx'

```

Play a bit on the command line, use your own variables!

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [April 6, 2022, 8:12am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/16 "2022-04-06T08:12:00Z")

</div>

some 'reference' sources

if you REALLY want to help yourself [and colleagues] invest in a [couple] of paperbacks  
(O'Reilly) Korn shell books are available on amazon/ebay for a fraction of their original selling prices

```nohighlight
https://www.qenesis.com/training/shell_programming/kshCheatsheet.pdf
http://kalwin.fr/docs/pdf/unix/unix-korn-shell.pdf (en francais - but you'll get the gist)
http://www.st23.org/misc/sysadmin/downloads/reference.card-ksh.programming.pdf
https://jkmaterials.yolasite.com/resources/materials/UNIX/UNIX_SHELL_PROGRAMMING/UNIT-IV-%20USP.pdf
https://geodesy.geology.ohio-state.edu/course/refpapers/korn_script_0105.pdf
https://azizsaya.wordpress.com/2018/03/24/korn-shell-cheat-sheet/

```

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [June 5, 2022, 8:12am UTC](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/17 "2022-06-05T08:12:05Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
