LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 01-07-2008, 11:23 AM   #1
jimmyjiang
Member
 
Registered: Jun 2006
Posts: 132

Rep: Reputation: 15
How to concatenate two strings into one string in B-shell?


hi,
I have trouble to put 2 string together.what I need is a date format like:Jan 7 09

so I wrote a small script:

#!/bin/sh
v1= date |cut -d " " --fields=2,3,4
echo $v1

v2= date |cut -d " " --fields=5 |cut -d: -f1
echo $v2

v3 = ${v1} ${v2}
echo $v3

the output is:
[root@96742-migoi8 ~]# ./maillog.sh
Jan 7

12

./maillog.sh: line 9: v3: command not found

what 's wrong with my script?
thanks in advance!
jimmy
 
Old 01-07-2008, 01:03 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well... there are some issues in your script. Let's see...
Code:
#!/bin/sh
v1= date |cut -d " " --fields=2,3,4
echo $v1
1. when assigning variables you should not put any blank space around the sign =.
2. to assign the output of a command to a variable (command substitution) you have to use the proper syntax. In the Bourne shell /bin/sh, this is done by sourronding the command with back ticks, as in `command`. In the Bourne Again shell /bin/bash, you can use also the syntax $(command). So these first lines will be:
Code:
#!/bin/sh
v1=`date | cut -d " " --fields=2,3,4`
echo $v1
The same apply to the second part
Code:
v2=`date | cut -d " " --fields=5 | cut -d: -f1`
echo $v2
3. to do string concatenation you have to put together the originating strings (as you already tried to do), but again don't leave any space around the sign =. Furthermore, if you want a blank space between the two strings you should use double quotes:
Code:
v3="$v1 $v2"
echo $v3
Just a note: to debug scripts you can run using
Code:
sh -x script.sh
or bash -x if using the Bourne Again shell. This will give you a trace of all the commands executed by the shell, together with the variable or command substitutions.
 
Old 01-07-2008, 02:36 PM   #3
jimmyjiang
Member
 
Registered: Jun 2006
Posts: 132

Original Poster
Rep: Reputation: 15
thanks, it works! but has one prolbem, how can I make 2 space btwn "Jan" and "7"? I tried:v3="$v1 $v2", but not work.
 
Old 01-07-2008, 04:35 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can preserve multiple spaces by quoting the variable:
Code:
v1=hello
v2=world
v3="$v1       $v2"
echo $v3            # output: hello world
echo "$v3"          # output: hello       world
 
Old 01-08-2008, 11:18 AM   #5
jimmyjiang
Member
 
Registered: Jun 2006
Posts: 132

Original Poster
Rep: Reputation: 15
it works great. thanks a lot!
 
Old 01-08-2008, 01:15 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome! Just a side note: you can obtain the same result using "format specification" in the command "date", e.g.
Code:
$ date +"%b %e  %H"
Jan  8  20
See man date for the various format specifications. Also look careful at the spaces and extra characters inside the quotes. For examples the following are valid formats, but give different results:
Code:
date +"%b %e  %H"
date +"%b%e%H"
date +"%b, day = %e  hour = %H"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
compare strings in shell scripting culin Linux - General 10 01-24-2013 01:33 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Need shell script to concatenate a string and a variable into a directory name AwesomeMachine Linux - Newbie 2 05-07-2006 03:42 AM
[Bash] Concatenate string using awk senorsnor Programming 7 05-05-2005 12:38 AM
PL/PGSQL - String concatenate problem jaunine Linux - Software 7 03-18-2005 07:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 10:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration