How to concatenate string containing a leading dash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to concatenate string containing a leading dash?
# 1  
Old 12-05-2009
Question How to concatenate string containing a leading dash?

Is there a way to concatenate two strings, where the first string is "-n" and there is a space between the "-n" and the second string? Below are some examples of what I tried.
Code:
#!/bin/sh
var1=test

#working without dashes:
var2="n $var1"
echo $var2

var2=n" "$var1
echo $var2

var2="n "$var1
echo $var2

#not working with dashes:
var2="-n $var1"
echo $var2

var2=-n" "$var1
echo $var2

var2="-n "$var1
echo $var2

#not working with escaped dashes:
var2="\-n $var1"
echo $var2

var2=\-n" "$var1
echo $var2

var2="\-n "$var1
echo $var2

Thank you.
# 2  
Old 12-05-2009
hi..
"-" is a shell metacharacter . So it needs strong quoting.
trying using single quotes '-n' or escape is using '\'.
Hope that works for you .
Regards.
# 3  
Old 12-05-2009
whish shell are you using??
because i am not facing any problem in concat
Code:
home/fnsonlq0>var=test
home/fnsonlq0>var1="-n "$var
home/fnsonlq0>echo "$var1"
-n test
home/fnsonlq0>

# 4  
Old 12-05-2009
vidyadhar85,
I am using the Debian Almquist shell (came with Ubuntu 9.04).

gaurav1086,
I retried with single quotes, but got the same results.
Code:
#not working with dashes
var2='-n $var1'
echo $var2

var2=-n' '$var1
echo $var2

var2='-n '$var1
echo $var2

#not working with escaped dashes
var2='\-n $var1'
echo $var2

var2=\-n' '$var1
echo $var2

var2='\-n '$var1
echo $var2

# 5  
Old 12-05-2009
Code:
var="-n"
printf -- "$var\n"

# 6  
Old 12-05-2009
Thanks for all your suggestions. The echo string needed quotes. The following examples worked.
Code:
#!/bin/sh

#concatinating with leading dash  
#All output "-n test" (echo string needs quotes)

var1=test

var2="-n $var1"
echo "$var2"

var2=-n' '$var1
echo "$var2"

var2='-n '$var1
echo "$var2"

var2=\-n' '$var1
echo "$var2"

printf -- "$var2\n"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. UNIX for Dummies Questions & Answers

Inspecting leading char in string for slash

In a SCO Unix shop, I am working on the following script to move any file to its same location on the target machine (called 'othersy' here): pwd=`pwd` for i in "$@" do echo " $i " if ; then echo 1; else echo 0; fi rcp -p $i othersy:$pwd/$i echo "Finished with ^ If I find a file... (4 Replies)
Discussion started by: wbport
4 Replies

3. Shell Programming and Scripting

Strip leading and numbers from a string.

Hello I have two vars loaded with $VAR1="ISOMETHING103" $VAR2="COTHERTHING04" I need to: 1) Strip the first char. Could be sed 's/^.//' 2) The number has it's rules. If it has "hundreds", it needs to be striped. If it is just two digits it shouldn't. So, for VAR1 output should be... (7 Replies)
Discussion started by: tristezo2k
7 Replies

4. Shell Programming and Scripting

Extracting string before first dash in a file name

Hi all, Given a file name such as EXAMPLE=lastname-02.30.71-firstname-town-other.tar.gz How do I print everything before the first dash (i.e. lastname) Note: I do not know exactly how many dashes or what information there will be in each file name so it is important that the code... (2 Replies)
Discussion started by: bashnewbee
2 Replies

5. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

6. Shell Programming and Scripting

How to concatenate a string and a variable

I need a way to build variable in this manner: variable_$i Inside a for loop i need to create it. where i goes from 1 to 30.. and then i need to print them on screen with echo $variable_$i which is the best way to do this? (6 Replies)
Discussion started by: sreedivia
6 Replies

7. UNIX for Dummies Questions & Answers

awk to seperate a string that has a dash

Hello I have this string XYZ-ABC DFT-ERT QWE-TYU I want to get the part after the dash. how to do that? thanks (2 Replies)
Discussion started by: melanie_pfefer
2 Replies

8. UNIX for Dummies Questions & Answers

trim leading zero in certain column in a string

I would like to know how to trim leading zero only in certain column of of a string, example: hdhshdhdhd000012mmmm0002abc <===== before hdhshdhdhd 12mmmm 2abc <===== after Thanks for your help. (2 Replies)
Discussion started by: dngo
2 Replies

9. UNIX for Advanced & Expert Users

How to remove a file with a leading dash '-' in it's name?

Somehow someone created a file named '-ov' in the root directory. Given the name, the how was probably the result of some cpio command they bozo'ed. I've tried a number of different ways to get rid of it using * and ? wildcards, '\' escape patterns etc.. They all fail with " illegal option --... (3 Replies)
Discussion started by: GSalisbury
3 Replies
Login or Register to Ask a Question