display strings on same line

Hi All.

This is more of a terminal scripting question than an apple script one, but hope you can help.

I want to display two strings on the same line so example I have:

v1=hello
v2=world
output=$v1." ".$v2
echo $output

what I get as a result is:

. .world

were what I want is:

hello world

Any ideas how to solve this?

Hi,

Have you tried:

v1=hello
v2=world
output= “$v1 $v2\n”
echo $output

I get “hello. .world” on a line by itself. What shell are you using? (ps -axo command -p $$ | sed 1d)

Short: Use echo “$v1 $v2” or echo “$v1” “$v2” (or output=“$v1 $v2”; echo “$output”.

It looks like you are using a Perl-like syntax there, but ˜the shell’ (typically bash on Mac OS X 10.3+) does not support that syntax. Like most shells in the Bourne family, bash automatically concatenates adjacent strings. Though unlike more formal programming languages, the contents of a variable can be split into multiple words unless it is quoted. Usually this is only a problem if you have whitespace (spaces, newlines, returns, tabs, etc.) in the value of a variable.

So, a safe version of what you want is output=“$v1"” “”$v2", where each variable and the combining space is quoted individually. Based on the fact that there is no (unquoted) intervening whitespace, bash creates a single string value (“hello world”) and saves it in the output variable. But since we are putting double quoted strings next to double quoted strings, we can just coalesce them into a single double quoted string: output=“$v1 $v2” (nearly what Martin shows). According to this concatenation rule, ‘foo bar’, 'foo 'bar, foo’ bar’, and foo\ bar (among other variations) are all equivalent and result in the same string value (the words ˜foo’ and ˜bar’ with a space between).

One detail of Martin’s example (output=“$v1 $v2\n”, I took out an extra space between the equal sign and double quote that was causing a problem) is that the blackslash-en is still those two literal characters in the string value stored in output. This is different from C, Perl and many other languages where the value of output would end in an actual line feed instead of a lowercase N. The potential complication is that there are lots of variations (across OSes, shells, and external versions) of how echo handles escape sequences. It is generally held to be more portable to avoid using escape sequences with echo. Generally the printf command is a better specified, more portable way of including such escape sequences.

[code]env PS1=‘$ ’ PS2=’’ /bin/bash --norc
$ v1=hello
$ v2=world
$ output=$v1." “.$v2
$ moutput=”$v1 $v2\n"
$ coutput=“$v1 $v2”
$ noutput=“$v1
$v2”
$ echo $output
hello. .world
$ echo $moutput
hello world\n
$ shopt xpg_echo
xpg_echo off
$ echo -e $moutput
hello world

$ echo $coutput
hello world
$ echo $noutput
hello world
$ echo “$noutput”
hello
world[/code]
Here, moutput has the backslash-en sequence at the end. In my demonstration shell (bash 2.05 with the xpg_echo option disabled), I have to pass echo an extra -e option to get it to translate the escape sequence. Also, (unless given the -n option (or in some variations, a last argument ending in backslash-cee)) echo will add its own newline after whatever else it prints (thus the extra blank line). noutput has an actual embedded newline (since the double quote was still active when I pressed return while giving its value). But, to preserve the embedded newline, the variable must be quoted. Otherwise, the shell interprets the newline as a ˜word break’ and thus supplies echo with two arguments (which it prints separated by a space) instead of a single argument with an embedded newline.

The rule of shell programming is to always double quote your variable expansion to be sure to preserve any embedded whitespace.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4.0.4 (4531.21.10, r53456)
Operating System: Mac OS X (10.4)