concatenate strings in bash
February 12th, 2007 by Lawrence David
let’s say you’ve got:
$j=”foo”
and you’d like to tack on its good partner ‘bar’. then, use the dollar sign and curly braces to delineate variable from string:
$k=”${j}bar”
February 12th, 2007 by Lawrence David
let’s say you’ve got:
$j=”foo”
and you’d like to tack on its good partner ‘bar’. then, use the dollar sign and curly braces to delineate variable from string:
$k=”${j}bar”
Thank a lot
holy crap, thanks a lot man. a search on Google reveals nothing but extremely bad examples and explain nothing.
Bulls eye.. I just got exactly what I needed.
I think it should be like this:
k=“${j}barâ€
nice catch ben; duly updated!
Thanks!
thanks!! Very helpful!!
IMHO a simpler solution would be:
#!/bin/bash
j=”foo”
k=”bar”
concatenation=”$j$k”
echo j: $j
echo k: $k
echo j+k: $concatenation
This will produce:
j: foo
k: bar
j+k: foobar
Outstanding!
Just leave out those citation marks…
[jakob@kage ~]$ j=â€fooâ€
[jakob@kage ~]$ k=â€barâ€
[jakob@kage ~]$ concatenation=â€$j$kâ€
[jakob@kage ~]$ echo $concatenation
â€â€fooâ€â€barâ€â€
^^
you don’t get into trouble until you stat with
complicated file names –
PTH=”/work/dir/test”
DTE=”2008/10/31″
FILENAME=${PTH}/${DTE}_suffix.out
this practice guarantees it will work
Great tip, that will be easy to remember.
May you live to be a thousand years old. (Thank you for the help.)
i want to use for loop for assign:
for i in 0 1 2 3
do
id$i=`cat file | grep list$i`
done
then:
cat file | grep list$i – work well
but assign bring error
-sh: id0=: not found
edi,
You need to typeset id$i like this:
typeset id$i=”cat file | grep list$i”
Thank you for your very helpfup info on joining strings in shell scripts.
dammit!
one of the most useful things i’ve read today!
Here is where you absolutly need this tip…$k=â€${j}barâ€
copy/paste to /tmp/test.sh
=========================================
#!/bin/bash
# test.sh
APPNAME=mediawiki
clear
cat << EOF
Here’s what we get….
\$APPNAME=mediawiki
We want to concantinate the value of \$APPNAME and user to be mediawikiuser
MYSQLUSER=\${APPNAME}user << subs the var value & concantenates correctly
MYSQLUSER=${APPNAME}user << this is what we want
MYSQLUSER=\$APPNAMEuser << this doesn’t work, we’re subbing the var name \$APPNAME
MYSQLUSER=$APPNAMEuser < /tmp/test_2.sh << EOF
#!/bin/bash
# test_2.sh
APPNAME=mediawiki
clear
cat << EOF
We want to concantinate the value of \$APPNAME and user to be mediawikiuser
MYSQLUSER=${APPNAME}user < /tmp/test_2.sh << EOF
#!/bin/bash
# test_2.sh
APPNAME=mediawiki
clear
echo “MYSQLUSER=${APPNAME}user << this is what we want”
echo “MYSQLUSER=\${APPNAME}user << this is what’s between the cat EOF’s”
MYSQLUSER=\${APPNAME}user # Assign \${APPNAME}user to MYSQLUSER
echo -e “\n \$MYSQLUSER << the result of MYSQLUSER when we run /tmp/test_2.sh\n”
echo -e “\nCool…!!\n\n”
exit
EOF
chmod 770 /tmp/test_2.sh
read -p “Press return to run /tmp/test_2.sh” input
/tmp/test_2.sh
echo -e “\n\n Here’s the contents of /tmp/test_2.sh as seen above…\n\n”
cat /tmp/test_2.sh
cat << EOF
So what we have done here is to generate a script form a script and substituted var values and var names.
Each with different results…when we pipe through cat
Interesting…
EOF
exit
=========================================
I want to run a program in which i will get say for eg; 6 output.I want each output in different files.
My output file should be file1.txt
file2.txt
file3.txt
Each time i want the output file created automatically with the nos alone changing.
Hi
i am facing two problems and I guess this is the best place to get help from.
i am using bash and need to write a bash script.
Problem-1
I have two file t1.txt and t2.txt. each with 100 lines. I am trying to make 100 file that each line of file t2.txt is concatenated with line number 1 or t1.txt and file is named as o1.txt. then each line of t2.txt is concatenated with 2nd line of t1.txt and saved as 02.txt and so on till all lines in t1.txt are exhausted.
problem-2
I have a number of CDIR in format of 12.458.0.0/24 etc.
I need a bash script that do following:-
delete all character on right of / in each line.
if all character on left side of / till it hits a . while deleting towards left.
if character on left of . is a 0 then delete both 0 and “.”.
I guess its a challenging question. but i am looking forward for a guidance.
thanks in advance
Thank you so much for this. Saved me hours.
Wow.. 3 years old and still helping people. Good tip.
Thanks! Google throws up tons of useless answers to this problem. You just saved me half an hour.
thanks. Cool navigation menu btw
Hi
I have a heap of duplicate files (avi and jpegs etc).
File names are often sequencenumber.avi, or sequencenumber.jpg
The parent directory or a directory in the path contains a better description of what the file is.. eg 2008 Files off Camera/trip to paris/R0001.avi (many files look like this.
In those cases I would want the file name to be changed to trip to paris 0001.avi etc.
any idea how I might do this?
L
I would do it something like this. Modify it to meet your requirements. Script does not check the input but it could be done with grep and with the same regural expression what the Filename is using. You can make it more efficient by giving the folder with a parameters. But here is a small help.
FOLDER=`pwd`
cd $FOLDER
TRIPNAME=`basename ${FOLDER}`
for i in *.avi;do
if [ -f $i ]; then
FILENAME=`echo $i |sed ‘s_R\([0-9]*.avi\)_\1_’`
echo “mv $i ${TRIPNAME}${FILENAME}”
mv $i ${TRIPNAME}${FILENAME}
fi
done
And for the catenation I used it like this. Tarstring is used as a part of larger command string.
for i in ../package/*.jpg;do
if [ -f $i ]; then
filename=`basename ${i}`
tarstring=${tarstring}” “${filename}
fi
done
Hi!
How do I get the result of the first line and the second line concatenated into one line and added to a text file, not onto two lines like this does it?
ls ./$aa.*|wc -l >> crds.txt;
echo “|”$aa >> crds.txt
instead of
2347
|agency
it should write
2347|agency
Thanks!
Helmut
Your example works in the case that both variables being concatenated need to be dereferenced at the time of assignment. In the case that you’d like to concatenate variables and not dereference the values. For example, if you are building up the text of a command to be run in the future
$format= “$1″
$date_cmd= $(echo ‘date ‘ “+$format”)
Now you can run $date_cmd as a command and it will evaluate each time.
thank you very much
yay
cheers! ^^
[...] I didn’t even know why you need to use ‘export’. I didn’t know how to concatenate strings in a bash variable. Or, even how to run a command and capture the output in a variable. I also didn’t know how [...]
Thanks for the tip..